繁体   English   中英

如何获得所有类别的产品,但产品A必须来自类别1

[英]How to get products from all categories but the product A must be from cateogry 1

我想进行一次SQL查询,以获取所有类别的所有产品。 “产品A”存在多个类别。 我只需要“类别1”中的产品A和所有其他产品。 这是我的数据库表。

+------+------------+------------+
| id   |  category  | product    |
+------+------------+------------+
|    1 | 1          | Product A  | 
|    2 | 2          | Product B  |
|    3 | 3          | Product C  |
|    4 | 4          | Product A  |
|    5 | 5          | Product F  | 
|    6 | 6          | Product D  |
|    7 | 7          | Product A  | 
+------+------------+------------+

预期的结果应该是

+------+------------+------------+
| id   |  category  | product    |
+------+------------+------------+
|    1 | 1          | Product A  | 
|    2 | 2          | Product B  |
|    3 | 3          | Product C  |
|    4 | 5          | Product F  | 
|    5 | 6          | Product D  |
+------+------------+------------+

你可以用工会

select category, product 
from my_table 
where product != 'Product A'
union 
select category, product 
from my_table 
where product =  'Product A'
and category = 1;

在你的情况下

select * from videos 
where trim(submitted ) != 'video_artist' 
union 
select * from videos 
where Trim(submitted) = 'video_artist' 
and category = 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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