Guest Posted July 21, 2003 Posted July 21, 2003 Sound like a fine idea to me. One question that has bugged me over the past couple of days, maybe you can answer: select p.products_id, p.products_image, max(o.date_purchased) max_date_purchased from .... group by p.products_id .... My understanding is that this is in fact invalid SQL code? How is your database going to know which one of the p.products_image entries it is going to show, since you are grouping by p.products_id? How does MySQL deal with, cases like this? Cheers, Rob
Guest Posted July 22, 2003 Posted July 22, 2003 Default OSC product record only has one image in it so there is no issue with that query.
Guest Posted July 22, 2003 Posted July 22, 2003 I understand that whatever record MySQL chooses, doesn't matter in this case. My question was slightly more off-topic, how does MySQL handle this in general? Would the query fail in case of different values? But this is off-topic for this forum, so I guess I'll go and find a manual. Cheers, Rob
Guest Posted July 22, 2003 Posted July 22, 2003 No... It should order the records retrieved by products_id... productid_1 record1 productid_1 record2 productid_1 record3 productid_2 record1 productid_2 record2 productid_2 record3 Group By is to allow you to group records by a common field. Any singular field should be automatically joined to each record that it fits. Try the exact query that you are referring to in phpMyAdmin and look at the results..
Guest Posted July 22, 2003 Posted July 22, 2003 Group By is to allow you to group records by a common field. Any singular field should be automatically joined to each record that it fits. I know that, but isn't the 'proper' way to do this to include every field by either in the GROUP BY or used in an aggragate function? Cheers, Rob
Daemonj Posted July 23, 2003 Posted July 23, 2003 Moved to General Chit-Chat by DaemonJ as the more appropriate forum. "Great spirits have always found violent opposition from mediocre minds. The latter cannot understand it when a man does not thoughtlessly submit to hereditary prejudices but honestly and courageously uses his intelligence." - A. Einstein
wizardsandwars Posted July 23, 2003 Posted July 23, 2003 Hi Guys, The reason that the original query is invallid is because *all* of the column not being aggregated must be present in the group by clause. select p.products_id, p.products_image, max(o.date_purchased) max_date_purchased from .... group by p.products_id, p.products_image, This works GREAT! ------------------------------------------------------------------------------------------------------------------------- NOTE: As of Oct 2006, I'm not as active in this forum as I used to be, but I still work with osC quite a bit. If you have a question about any of my posts here, your best bet is to contact me though either Email or PM in my profile, and I'll be happy to help.
Guest Posted July 24, 2003 Posted July 24, 2003 That was the point I was trying to make, thanks for the clarification. I was asking because I was running into this problem with my postgresql backend. I was wondering wether I and postgresql were both wrong and that any columns not in the GROUP BY or an aggregate could be handled a third way. But it seems that MySQL is just very forgiving wrt handling improper SQL. Thanks, Rob
wizardsandwars Posted July 24, 2003 Posted July 24, 2003 Yeah, actually, there are a few other DBMS that will allow you to leave out one or more of the non aggregated columns in the where clause. The reason is because of what is called "Table Folding" Table folding is when you purposly create a cartisean product to procuce a result set that includes complicated groupings. Let me know if you would like more information on when 'Table Folding' would be appropriate, and I'll forward you a demonstartation. ------------------------------------------------------------------------------------------------------------------------- NOTE: As of Oct 2006, I'm not as active in this forum as I used to be, but I still work with osC quite a bit. If you have a question about any of my posts here, your best bet is to contact me though either Email or PM in my profile, and I'll be happy to help.
Guest Posted July 24, 2003 Posted July 24, 2003 The reason is because of what is called "Table Folding" Table folding is when you purposly create a cartisean product to procuce a result set that includes complicated groupings. Since this thread got moved to chitchat, I won't be bashful. I don't see how leaving out a column in a group by would matter, if the left out column is known to have the same values you could include them in the GROUP BY in the first place, if not the DB has to make some decision on you behalf which makes the result inpredictable so it would seem that you dan't care about the result and leaves me to wonder why you're selecting that column in the first place. (Performance might be better when the DB just spits out a random result from the GROUPed records for that column?). I did a google on "Table Folding" but I fail to see it's impact/ relevance on column grouping. So, in short, I could probably learn something here, I'd love a deonstration. Cheers, Rob
Recommended Posts
Archived
This topic is now archived and is closed to further replies.