fetch mysql information based on column value

Is there a way to run a mysql $query and specify and order by the value within a column. so if you have a table with column:name, and there are 2 people with the name 'john' but the first john has 'id':10 and the second john has 'id':20. I want to echo the first john(10) first. so something like:

  $sql = mysql_query("SELECT firstname FROM users 
                   WHERE email='$email' 
                   AND id='(*smallest value*)'");

  while($info = mysql_fetch_assoc($sql))
  echo $info['firstname']

preguntado el 31 de julio de 12 a las 14:07

Creo que necesitas usar el order by cláusula -

3 Respuestas

SELECT firstname FROM users WHERE email='$email' order by id asc limit 1

Respondido 31 Jul 12, 14:07

Trata

SELECT firstname FROM users WHERE email='$email' order by id asc limit 1

Respondido 31 Jul 12, 14:07

 $sql = mysql_query("SELECT firstname FROM users 
               WHERE email='$email' 
               ORDER BY id ASC");

will give you the John(10) before the John(20).

 $sql = mysql_query("SELECT firstname FROM users 
               WHERE email='$email' 
               ORDER BY id ASC
               LIMIT 1");

will give you only John(10).

Respondido 31 Jul 12, 14:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.