debuitls Posted September 7, 2009 Share Posted September 7, 2009 Hi all, This is an extension of a thread below which ran yesterday in php coding forum! But I dont expect anyone to read through the 27 entries so just gonna summarise here and see if anyone with a fresh pair of eyes today can make any suggestions! Bascially I have two tables bid table and proposal table. I'm trying to print to screen all the proposals made by a particular user! The problem is that only those proposals that have bids made on them are printing to screen. So for example if four proposals are made by a user, I want all four to be printed, however currently if only two of them have bids on them there the only ones that are getting printed. I'm pretty sure its something to do with the group by statement as when I remove it, it seems to work fine, but I consequently lose the critical "count bids" info. Anyway here's the code. SELECT proposal . * , SYSDATE( ) , GROUP_CONCAT( bid.proposalid ) AS 'bids', TIME_FORMAT( TIMEDIFF( tomorrowtime, SYSDATE( ) ) , '%H hours, %i minutes' ) AS timeleft FROM proposal LEFT OUTER JOIN bid ON proposal.proposalid = bid.proposalid WHERE NOW( ) < proposal.tomorrowtime AND proposal.username = '$username' GROUP BY bid.proposalid Been struggling with this for a while! If you think of anything please let me know! Quote Link to comment https://forums.phpfreaks.com/topic/173419-solved-problem-with-group-by-aspect-of-sql-statement/ Share on other sites More sharing options...
artacus Posted September 8, 2009 Share Posted September 8, 2009 Change GROUP BY bid.proposalid to GROUP BY proposal.proposalid You are grouping by a field that will be NULL when there are no bids. Also, you should have posted this in the mysql forum. Quote Link to comment https://forums.phpfreaks.com/topic/173419-solved-problem-with-group-by-aspect-of-sql-statement/#findComment-914927 Share on other sites More sharing options...
artacus Posted September 8, 2009 Share Posted September 8, 2009 And thirdly GROUP_CONCAT( bid.proposalid ) AS 'bids' doesn't make sense. You'll get a comma separated list of all the same values. Maybe count() is what you want? Quote Link to comment https://forums.phpfreaks.com/topic/173419-solved-problem-with-group-by-aspect-of-sql-statement/#findComment-914930 Share on other sites More sharing options...
debuitls Posted September 8, 2009 Author Share Posted September 8, 2009 Thanks artacus for getting back to me! I saw my mistake last night and altered and it worked fine! (Should have marked this resolved) As you said I was grouping by bid.proposalid when I should have been grouping by proposal.proposalid. I was using GROUP_CONCAT( bid.proposalid ) and then then echoing count(",",explode($row['bids'])); I posted this is in mysql forum also but no one got back to me! Anyway, thanks very much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/173419-solved-problem-with-group-by-aspect-of-sql-statement/#findComment-914954 Share on other sites More sharing options...
debuitls Posted September 8, 2009 Author Share Posted September 8, 2009 My mistake sorry I didnt post this in mysql forum. Apologies. Quote Link to comment https://forums.phpfreaks.com/topic/173419-solved-problem-with-group-by-aspect-of-sql-statement/#findComment-914956 Share on other sites More sharing options...
artacus Posted September 8, 2009 Share Posted September 8, 2009 I was using GROUP_CONCAT( bid.proposalid ) and then then echoing... I figured that was what you were doing. That's just making more work for mysql and php. Just do it in sql like so: COUNT( bid.proposalid ) AS bids And don't sweat posting here. You wouldn't have gotten an answer from me as I never bother with the mysql forum. There's too many numbskulls asking PHP questions there because they don't understand the difference between PHP and MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/173419-solved-problem-with-group-by-aspect-of-sql-statement/#findComment-915080 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.