Jump to content

Mixing 3 queries


Canman2005

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.