Author Topic: [SOLVED] problem with 'group by' aspect of sql statement!  (Read 1467 times)

0 Members and 1 Guest are viewing this topic.

Offline debuitlsTopic starter

  • Enthusiast
  • Posts: 64
  • Gender: Male
    • View Profile
[SOLVED] problem with 'group by' aspect of sql statement!
« on: September 07, 2009, 10:46:29 AM »

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.

Code: [Select]
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!

Offline artacus

  • Devotee
  • Posts: 915
  • Gender: Male
  • World domination? Never heard of it.
    • View Profile
    • Flex and Specs()
Re: problem with 'group by' aspect of sql statement!
« Reply #1 on: September 08, 2009, 03:30:34 PM »
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.
artacus

-- Trust your leaders, depend on your government, don't question the media... and this won't hurt a bit. --

Offline artacus

  • Devotee
  • Posts: 915
  • Gender: Male
  • World domination? Never heard of it.
    • View Profile
    • Flex and Specs()
Re: problem with 'group by' aspect of sql statement!
« Reply #2 on: September 08, 2009, 03:33:51 PM »
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?
artacus

-- Trust your leaders, depend on your government, don't question the media... and this won't hurt a bit. --

Offline debuitlsTopic starter

  • Enthusiast
  • Posts: 64
  • Gender: Male
    • View Profile
Re: problem with 'group by' aspect of sql statement!
« Reply #3 on: September 08, 2009, 04:03:34 PM »
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

Code: [Select]
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!

Offline debuitlsTopic starter

  • Enthusiast
  • Posts: 64
  • Gender: Male
    • View Profile
Re: [SOLVED] problem with 'group by' aspect of sql statement!
« Reply #4 on: September 08, 2009, 04:05:44 PM »
My mistake sorry I didnt post this in mysql forum.

Apologies.

Offline artacus

  • Devotee
  • Posts: 915
  • Gender: Male
  • World domination? Never heard of it.
    • View Profile
    • Flex and Specs()
Re: [SOLVED] problem with 'group by' aspect of sql statement!
« Reply #5 on: September 08, 2009, 05:58:47 PM »
Quote
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:

Code: [Select]
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.
artacus

-- Trust your leaders, depend on your government, don't question the media... and this won't hurt a bit. --