Garethp Posted March 22, 2011 Share Posted March 22, 2011 Hey guys, I'm having a little problem. I have three tables. sites, devices, and device_status. Basically, device_status holds multiple rows per device, of when the device last pinged on of our servers, and each site can have multiple sites. So what I'm trying to do is find when a site was first created, but finding when it's devices first pinged us, and get all sites set up since October. What I tried was SELECT sites.*, MIN(device_status) as firstcontact FROM sites LEFT JOIN devices ON devices.siteid=sites.id LEFT JOIN device_status on device_status.deviceid=devices.id GROUP BY sites.id HAVING firstcontact > UNIX_TIMESTAMP('2010-08-01') ORDER BY firstcontact DESC But it shows no sites, and only two sites when I change it to January of 2010. My guess is that this is a result of the GROUP BY sites.id, and I was thinking that if I could somehow group by devices and sites it might show the correct results. How would I do this? Quote Link to comment https://forums.phpfreaks.com/topic/231354-multiple-group-bys/ Share on other sites More sharing options...
fenway Posted March 29, 2011 Share Posted March 29, 2011 Sorry, I don't follow. Quote Link to comment https://forums.phpfreaks.com/topic/231354-multiple-group-bys/#findComment-1193917 Share on other sites More sharing options...
kickstart Posted March 31, 2011 Share Posted March 31, 2011 Hi You should use all the non aggregate fields in the SELECT within the GROUP BY. However MySQL is pretty tolerant of not doing this (most flavours of SQL would just error). Is device_status really a column with a date in it? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/231354-multiple-group-bys/#findComment-1194932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.