Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql在同一查询中使用多个不同的where条件进行多次选择_Mysql_Where Clause_Subquery_Multiple Select - Fatal编程技术网

Mysql在同一查询中使用多个不同的where条件进行多次选择

Mysql在同一查询中使用多个不同的where条件进行多次选择,mysql,where-clause,subquery,multiple-select,Mysql,Where Clause,Subquery,Multiple Select,我有下表: +--------------------+-----------+--------------------+ | Product | website | is_master | +--------------------+-----------+--------------------+ | product A | X | 1 | | product A

我有下表:

+--------------------+-----------+--------------------+
| Product            | website   | is_master          |
+--------------------+-----------+--------------------+
| product A          |       X   | 1                  |
| product A          |       Y   | 0                  |
| product A          |       Z   | 0                  |
| product A          |       C   | 0                  |
| product B          |       D   | 1                  |
| product C          |       E   | 1                  |
+--------------------+-----------+--------------------+
我正在尝试进行mysql查询,以获得如下表a:

+--------------------+------------------+--------------------+
| Product            | master_website   | additional_sites   |
+--------------------+------------------+--------------------+
| product A          |       X          | y,z,c              |
| product B          |       D          | null               |
| product C          |       E          | null               |
+--------------------+------------------+--------------------+
我尝试过使用subselect查询,但在这两种情况下都失败了

select 
Product, 
    (select Product 
     FROM `table1` 
     LEFT JOIN 
     table2 on table1.id = table2.fk_table1 
     WHERE is_master = 1) is_master, 

    (select group_concat(Product) 
     FROM `table1` 
     LEFT JOIN 
     table2 on table1.id = table2.fk_table1 
     WHERE is_master = 0) additional 

     FROM `table1` 
     LEFT JOIN 
     table2 on table1.id = table2.fk_table1 

WHERE 1 
group by 
Product 
问题是subselect返回多行


您可以在2个子选择1中断开联接查询,以便为每个产品仅选择一条记录,其中,is_master=1,我假设每个产品只有一行is_master=1,第二个子选择将选择其中,is_master=0的行,然后使用左联接进行子选择

SELECT t.*,t1.additional FROM 
(SELECT 
Product,
website AS is_master, 
FROM `table1` 
LEFT JOIN 
table2 ON table1.id = table2.fk_table1 
WHERE is_master = 1 
) t
LEFT JOIN 
(
  SELECT Product ,GROUP_CONCAT(website) additional
  FROM `table1` 
  LEFT JOIN table2 ON table1.id = table2.fk_table1 
  WHERE is_master = 0 
  GROUP BY Product
) t1 ON(t.Product = t1.Product)

我已经为站点为K的产品a添加了另一行,并且主控0为您的查询提供了错误的结果,它将K显示为主控website@uesr58967我更新了我的答案。这对你更有效吗?是的,现在看起来还可以:)通过这种“自我加入”技术,你为我打开了一个全新的世界!!谢谢,很好用!
SELECT t.*,t1.additional FROM 
(SELECT 
Product,
website AS is_master, 
FROM `table1` 
LEFT JOIN 
table2 ON table1.id = table2.fk_table1 
WHERE is_master = 1 
) t
LEFT JOIN 
(
  SELECT Product ,GROUP_CONCAT(website) additional
  FROM `table1` 
  LEFT JOIN table2 ON table1.id = table2.fk_table1 
  WHERE is_master = 0 
  GROUP BY Product
) t1 ON(t.Product = t1.Product)