1

I am trying to get the first id with a specific date. So this is my code:

$checkm = "SELECT FIRST(id) FROM times WHERE date='2014-03-07'";
$resultm = mysqli_query($con,$checkm);

I have a table called times and there are some columns. Two of them are date and id.

I am trying to get the first row's id with the date 2014-03-07.

EDIT: Fixed!

$checkm = "SELECT id FROM times WHERE date='2014-03-06' ORDER BY id ASC LIMIT 1";
$resultm = mysqli_query($con,$checkm); 

while($row = mysqli_fetch_array($resultm)) {
    $resultm1 = $row['id'];
}
Oscar Mederos
  • 28,017
  • 21
  • 79
  • 123
morha13
  • 1,723
  • 2
  • 19
  • 39

2 Answers2

2

You probably want the minimum id.

SELECT min(id) 
FROM times 
WHERE date = '2014-03-07'
jhmckimm
  • 697
  • 5
  • 15
Mike Sherrill 'Cat Recall'
  • 86,743
  • 16
  • 118
  • 172
  • so you quote my answer and vote another :P not that he's wrong, in fact his solution is more elegant for the given problem, but mine's more flexible incase you want to change how you would define "first" – Populus Mar 06 '14 at 19:37
0

pretty straightforward...

SELECT id FROM times WHERE date='2014-03-07' ORDER BY id ASC LIMIT 1
Populus
  • 7,136
  • 3
  • 37
  • 51
  • Thank you its working! script: $checkm="SELECT id FROM times WHERE date='2014-03-06' ORDER BY id ASC LIMIT 1"; $resultm=mysqli_query($con,$checkm); while($row = mysqli_fetch_array($resultm)) { $resultm1=$row['id']; } – morha13 Mar 06 '14 at 19:45
  • This Stack Overflow posting discusses the potentially significant differences in performance of using LIMIT vs MIN (or MAX). So be careful if you have chosen this route. http://stackoverflow.com/questions/426731/min-max-vs-order-by-and-limit – Megan Squire Mar 06 '14 at 20:33