Skip to content
Advertisement

sql, insert random data from selected data

sql, insert random data from selected data,

I get data from another table in the insertion process, but I only need to select one of these data randomly, now I can only add the first one of those data.

MySql DB.

I want “yorumlar2.id” to appear randomly. Only 1 pcs should come at random.

Advertisement

Answer

You can add the following to your query before your existing limit to randomise the order in which they’ll be fetched, leaving you with one random row.

If your table contains a high number of rows or you are more concerned with performance, then you might want to try this instead in your where clause:

and this as your ORDER BY/LIMIT:

This will calculate a random number under your highest ID, limit your query to rows above that ID, and then give you the first one, leaving you with one random row.

You can see this explained in detail here.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement