Determine the field in ORDER BY RAND () in mysql

3

Determine which table field will have ORDER BY RAND ()

Example: show random data according to field (category_id)

How do I put a: WHERE ORDER BY RAND () id_category ???

    
asked by anonymous 15.05.2014 / 16:05

2 answers

-1

Let's say your table has this structure:

CREATE TABLE 'cliente' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'nome' varchar(50) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=latin1$$

It would be SQL like this, because I want it to just rand () on id

SELECT a.id, b.nome from (SELECT id FROM cliente ORDER BY rand()) a join cliente b on b.id = a.id

In your case (example because you are totally unaware of its structure)

SELECT a.id_categoria, b.* FROM (SELECT id_categoria, a.id FROM tabela ORDER BY rand()) a join tabela b on b.id = a.id
    
15.05.2014 / 16:36
5

According to the documentation you can not get random results for one certain column.

  

You can not use a column with RAND () values in an ORDER BY clause,   because ORDER BY would evaluate the column multiple times. However,   you can retrieve rows in random order like this:

     

mysql> SELECT * FROM tbl_name ORDER BY RAND()

     

You can not use a column with RAND () values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times. However, you can retrieve rows in random order like this:

     

mysql> SELECT * FROM tbl_name ORDER BY RAND()

    
15.05.2014 / 16:21