Canman2005 Posted November 14, 2008 Share Posted November 14, 2008 Hi all I have 3 QUERIES, they are SELECT * FROM `log` WHERE `unique_id` = '51006401' AND `type_type` = 0 GROUP BY `product_id` SELECT * FROM `log` WHERE `unique_id` = '51006401' AND `type_type` = 3 GROUP BY `type_uid` SELECT * FROM `log` WHERE `unique_id` = '51006401' AND `type_type` = 4 GROUP BY `type_uid` Is it possible to combine all these 3 QUERIES into one? I have tired UNION and JOIN but cant seem to get it to work. Any help would be ace Thanks Dave Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/ Share on other sites More sharing options...
Mchl Posted November 14, 2008 Share Posted November 14, 2008 SELECT * FROM `log` WHERE `unique_id` = '51006401' AND `type_type` = 0 GROUP BY `product_id` UNION SELECT * FROM `log` WHERE `unique_id` = '51006401' AND `type_type` = 3 GROUP BY `type_uid` UNION SELECT * FROM `log` WHERE `unique_id` = '51006401' AND `type_type` = 4 GROUP BY `type_uid` ?? What exactly you want results to be? Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690078 Share on other sites More sharing options...
DarkerAngel Posted November 14, 2008 Share Posted November 14, 2008 Could I have more information on what exactly your trying to accomplish? I see that your trying to have the three queries combined into one but is there a specific reason behind this combination. SELECT * FROM `log` WHERE `unique_id` = '51006401' AND `type_type` = 0 OR `type_type` = 3 OR `type_type` = 4 GROUP BY `type_uid` Just something off the top of my head... You can have multiple comparisons for the same column. Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690081 Share on other sites More sharing options...
Mchl Posted November 14, 2008 Share Posted November 14, 2008 DarkAngel: Your query will never return any results. Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690082 Share on other sites More sharing options...
DarkerAngel Posted November 14, 2008 Share Posted November 14, 2008 lol yeah... your correct... I meant to put 'OR' Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690085 Share on other sites More sharing options...
Mchl Posted November 14, 2008 Share Posted November 14, 2008 lol yeah... your correct... I meant to put 'OR' Then you should probably add some parentheses as well SELECT * FROM `log` WHERE `unique_id` = '51006401' AND (`type_type` = 0 OR `type_type` = 3 OR `type_type` = 4) GROUP BY `type_uid` But as long as Canman2005 doesn't tell us what he wants, we can't be sure Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690089 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 Basically I have a log of products purchase, if value `type_type` equals 0 then it should return all rows but doing a GROUP BY `product_id` so that I only return products that do not have the same `product_id` If the value of `type_type` is equal to 3 or 4, then it should return all rows but do a group GROUP BY `type_uid` as that means those rows with the same `type_uid` are a group of products Does that make sense? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690102 Share on other sites More sharing options...
DarkerAngel Posted November 14, 2008 Share Posted November 14, 2008 Ok, but with what you said it would only make sense to logically combine the query if the "type_type" = 3 or 4, and leave it at the one query if it equals 0 Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690103 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 So is there no possible way to do it? Would I need to do 3 seperate QUERIES and not combine the results Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690104 Share on other sites More sharing options...
DarkerAngel Posted November 14, 2008 Share Posted November 14, 2008 here lets try this, give me your `log` structure and just some random sample data, I don't want to steal your code lol I have to much of my own to worry about, I just want to get a better feel of what your trying to accomplish and show you better queries. But: SELECT * FROM `log` WHERE `unique_id` = '51006401' AND (`type_type` = 3 OR `type_type` = 4) GROUP BY `type_uid` Should work to combine the GROUP BY `type_uid` queries, I still don't quite understand what your trying to do... lol but I'm trying to help. Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690108 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 Basically I have a log table which contains `product information` If a row contains the value 3 or 4 under the field `type_type` then it's classed as a group of products, so more than one product If a row contains the value 0 under the field `type_type` then it's classed as a single product Okay, so if any rows have 0 then they should be listed as normal but doing a GROUP BY `product_id` as I want to group together all single products with the same `product_id` value, basically so I can do a 'quanity' value if more than 1 of the same `product_id` is stored. If a row has 3 or 4 under `type_type` then I want to group the results together with GROUP BY `type_uid`, the reason is that you might have 10 products with the same `type_uid` code, that means they are a group of products and should be grouped together. Does that help anymore? Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690117 Share on other sites More sharing options...
Canman2005 Posted November 14, 2008 Author Share Posted November 14, 2008 So is the only method to do 3 seperate QUERIES? Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690235 Share on other sites More sharing options...
Mchl Posted November 14, 2008 Share Posted November 14, 2008 Two actually. One for type_type being 3 or 4 and another for type_type being 0 Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-690248 Share on other sites More sharing options...
fenway Posted November 17, 2008 Share Posted November 17, 2008 Ugh... you can't use * with GROUP BY...still. Quote Link to comment https://forums.phpfreaks.com/topic/132693-mixing-3-queries/#findComment-692154 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.