SELECT brings back wrong date

Hey,

I have the following SELECT:


    public function selectAll(){

        $sql = "SELECT *,i.ID as theID, i.date_added as theDate
                FROM c2_glo.tbl_courses i
                LEFT JOIN c2_glo.tbl_coursecategories c on c.ID = i.catID
                WHERE i.deleted = 0 AND c.deleted=0 GROUP BY RID ORDER BY date DESC";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

Now in my table i have a number of rows which i group by the RID. And from the GROUPED rows that i return i want to show the one where the date is the most recent.

So for example if ‘date’ had the following rows for example:

2010-07-03 00:00:00
2010-07-10 00:00:00
2010-07-17 00:00:00
2010-07-24 00:00:00

I would only want to show ‘2010-07-03 00:00:00’…

The sql statement i am using always shows ‘2010-07-24 00:00:00’, i have tried messing around with ASC and DESC but no luck…

Any ideas what i am doing wrong?

Thanks

You will definitely have issues just as soon as you have two people trying to do an add at the same time with them both getting the same RID for what should be different values.

Well yes, i mean if i can’t figure it out then i will come to you guys…

:wink:

do a search for race condition

why did you choose not to use auto_increment for this?

Oh well,

If i have any problems you guys will be the first to know… :lol:

Thanks for your help. :smiley:

Thanks r937,

Well i add multiple rows relating to the same course. I had to add a method of grouping the courses, so i added an RID.

Is this a problem? How would this haunt me in the future? :eek:

i think you should take responsibility for your own problems

:smiley:

Sorry it should be:


ORDER BY i.date DESC

Basically there multiple rows with the same RID, for example 23. Now in the admin section i only want to pull out one of those records by the most recent ‘date’…

My insert statement adds multiple rows but hold a different ‘date’, see below:


    public function insertCourse($category){

        $query = "SELECT MAX(RID) as Max_ID FROM tbl_courses 
        WHERE deleted = 0";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);

        $date = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'] ." ".$_POST['hours'].":".$_POST['minutes'].":00";
        $startdate = "0000-00-00 ".$_POST['starthours'].":".$_POST['startminutes'].":00";
        $enddate = "0000-00-00 ".$_POST['finishhours'].":".$_POST['finishminutes'].":00";
		$recWeeks = mysql_real_escape_string($_POST['recWeeks']);
		
		for($i=0; $i<$recWeeks; $i++){
		    $sql = "INSERT INTO tbl_courses
		        (catID, locID, RID, weeks, date, startdate, enddate, title, body, email, practitioner, phone, image, date_added) VALUES
		        (
		        '".mysql_real_escape_string($_POST['catID'])."',
		        '".mysql_real_escape_string($_POST['locID'])."',
		    	'".mysql_real_escape_string($row['Max_ID'])."' + 1,
		        '".mysql_real_escape_string($_POST['recWeeks'])."',
				'".mysql_real_escape_string($date)."' + INTERVAL ($i * 7) DAY,		        
				'".mysql_real_escape_string($startdate)."',	        
				'".mysql_real_escape_string($enddate)."',
		        '".mysql_real_escape_string($_POST['title'])."',
		        '".mysql_real_escape_string($_POST['body'])."',
		        '".mysql_real_escape_string($_POST['email'])."',
		        '".mysql_real_escape_string($_POST['practitioner'])."',
		        '".mysql_real_escape_string($_POST['phone'])."',
		        '".mysql_real_escape_string($_FILES['image']['name'])."',
		        now()
		        )";
        $result = mysql_query($sql) or die(mysql_error());
        }
        return "Successfully added course";
    }

It’s for a calendar type functionality. So if the following rows were inserted:

2010-07-03 00:00:00
2010-07-10 00:00:00
2010-07-17 00:00:00
2010-07-24 00:00:00

Each having the RID of 23, how can i only pull out the row with this date:

2010-07-03 00:00:00

As it’s the most recent…

Thanks

sorry, i was only going by what you were doing in the script, which was MAX(ID) + 1

i think you’re saying that you use this value multiple times

it’s still a potential race condition

SELECT i.ID as theID
     , i.locID
     , i.RID
     , i.weeks
     , i.date
     , i.startdate
     , i.enddate
     , i.title
     , i.body
     , i.email
     , i.practitioner
     , i.phone
     , i.image
     , i.date_added as theDate  
     , c.foo
     , c.bar
  FROM ( SELECT RID              
              , MAX(date_added) AS latest_date
           FROM c2_glo.tbl_courses 
          WHERE deleted = 0
         GROUP
             BY RID ) AS m
INNER
  JOIN c2_glo.tbl_courses AS i
    ON i.RID = m.RID
   AND i.date_added = m.latest_date
INNER
  JOIN c2_glo.tbl_coursecategories AS c 
    ON c.ID = i.catID 
   AND c.deleted = 0
 WHERE i.deleted = 0 

please note you are asking for a world of hurt by doing the MAX(ID)+1 calculation yourself – any reason you’re not using an auto_increment?

Well i thought it would not work because i would need the RID to be the same for more than one row?

This is a typical set of records in my table:

553
3
1
20
23
2010-11-13 00:00:00
0000-00-00 17:45:00
0000-00-00 19:00:00
Pregnancy Yoga
Pregnancy Yoga is…
anna@yoga4pregnancy.co.uk
Anna White is… 0844 800 7380
2010-06-15 00:30:39
0

552
3
1
20
23
2010-11-06 00:00:00
0000-00-00 17:45:00
0000-00-00 19:00:00
Pregnancy Yoga
Pregnancy Yoga is…
anna@yoga4pregnancy.co.uk
Anna White is…
0844 800 7380
2010-06-15 00:30:39
0

551
3
1
20
24
2010-10-30 00:00:00
0000-00-00 17:45:00
0000-00-00 19:00:00
Pregnancy Yoga
Pregnancy Yoga is…
anna@yoga4pregnancy.co.uk
Anna White is…
0844 800 7380
2010-06-15 00:30:39
0

So 23, 23, and 22 is the RID. So if i was to add multiple instances of the same row but with different days, would there have been a different method to grouping these together?

which table is the date column in? why are you using GROUP BY?

The GROUP BY RID in the select is what is causing the problem. With that there the rest of the query only sees one value for each field for each RID. If that is supposed to be there then you should also include all of the values except for RID that are being returned inside of functions that define which of the possible values it should select to return such as MAX()