0

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']
1
  • I believe you need to use the order by clause Commented Jul 31, 2012 at 13:11

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try

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

Comments

0
 $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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.