简体   繁体   中英

SQL - Combining Queries

I have two queries that do exactly as I want, however, I want to try combine these.

Query 1:

SELECT DISTINCT categories.id FROM categories 
    INNER JOIN product_categories 
        ON product_categories.category_id=categories.id 
    INNER JOIN products 
        ON product_categories.product_id=products.id 
    WHERE 
        categories.is_sub_cat='1' AND 
        categories.is_paused!='1' AND 
        products.nexus='1'

Query 2:

SELECT DISTINCT categories.top_cat FROM categories 
    INNER JOIN product_categories 
        ON product_categories.category_id=categories.id 
    INNER JOIN products 
        ON product_categories.product_id=products.id 
    WHERE 
        categories.top_cat!='0' AND 
        categories.is_paused!='1' AND 
        products.nexus='1'

Would it be possible to combine these into 1 query or should I just combine the resulting arrays in PHP after doing both queries?

You can use UNION to merge two queries

SELECT DISTINCT categories.id FROM categories 
    INNER JOIN product_categories 
        ON product_categories.category_id=categories.id 
    INNER JOIN products 
        ON product_categories.product_id=products.id 
    WHERE 
        categories.is_sub_cat='1' AND 
        categories.is_paused!='1' AND 
        products.nexus='1'
UNION 

SELECT DISTINCT categories.top_cat FROM categories 
    INNER JOIN product_categories 
        ON product_categories.category_id=categories.id 
    INNER JOIN products 
        ON product_categories.product_id=products.id 
    WHERE 
        categories.top_cat!='0' AND 
        categories.is_paused!='1' AND 
        products.nexus='1'

you can use case when

SELECT case when (categories.is_sub_cat='1' AND  categories.is_paused!='1' AND 
        products.nexus='1') then categories.id
        else categories.top_cat end  as id
    FROM categories 
    INNER JOIN product_categories 
        ON product_categories.category_id=categories.id 
    INNER JOIN products 
        ON product_categories.product_id=products.id 

You can combine the queries as

SELECT DISTINCT categories.id, categories.top_cat FROM categories 
    INNER JOIN product_categories 
        ON product_categories.category_id=categories.id 
    INNER JOIN products 
        ON product_categories.product_id=products.id 
    WHERE 
        ( categories.is_sub_cat='1' OR categories.top_cat!='0' ) AND 
        categories.is_paused!='1' AND 
        products.nexus='1'

Alternatively, UNION ALL might be used provided the columns categories.id and categories.top_cat are of the same data type, or able to be casted to the same data type.

Yes, UNION seems like a legit option. For your case it should not matter, since you are selecting from the same table, but as a side note you should know that when using UNION generally you need to have equal number of columns in your tables(that is if they were different).

You can do this with conditions in the WHERE clause:

SELECT DISTINCT c.id
FROM categories c INNER JOIN
     product_categories pc
     ON pc.category_id = c.id INNER JOIN
     products p
     ON pc.product_id = p.id 
WHERE p.nexus = 1 AND
      c.is_paused <> 1 AND
      (c.is_sub_cat = 1 OR
       c.top_cat <> 0 
      );

Note:

  • Table aliases make the query easier to write and to read.
  • Assuming that the columns are numbers, don't compare the values to strings. Try to avoid type conversion issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM