简体   繁体   中英

Merge two SQL tables with condition

I have start to learn SQL now and now my question: Look at my uploaded image and say me how I can filter all product categorys with the id_product = 45 and my return value is 45-3,45-2 and the row 1 from product groups.

Again: I will have ALL product_groups rows (1,2,3) and the related id_category(product_category) with the id_product = 45.

It's not easy to explain, sry for my bad English..

两个 SQL 表

Here is a SQL-Query who gives all rows with id from product_groups and id_product = 45 but NOT the id = 3 with empty values:-(

SELECT 
  id, title, multiplayer, id_product 
FROM 
  product_category 
RIGHT OUTER JOIN products_groups ON 
  rg_product_category.id_category = products_groups.id 
WHERE 
  product_category.id_product = '45' OR 
  product_category.id_product IS NULL AND products_groups.type = 'Game';

thanks for all help

I think I understand what you want: You want to list all product_groups, with null values if there's no match. Try this:

SELECT 
  id, title, multiplayer, id_product 
FROM  products_groups
LEFT JOIN product_category
  ON product_category.id_category = products_groups.id 
  AND product_category.id_product = '45'
WHERE products_groups.type = 'Game';

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