简体   繁体   English

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

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

I want to make a sql query to get all the products from all categories. 我想进行一次SQL查询,以获取所有类别的所有产品。 The "product A" exists in more than one cateogries. “产品A”存在多个类别。 I want product A only from "Category 1" and the all other products. 我只需要“类别1”中的产品A和所有其他产品。 Here, is my database table. 这是我的数据库表。

+------+------------+------------+
| 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  | 
+------+------------+------------+

And the expected resut should be 预期的结果应该是

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

You can use union 你可以用工会

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

in your case 在你的情况下

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