【问题标题】:How to get products from all categories but the product A must be from cateogry 1如何从所有类别中获取产品,但产品 A 必须来自类别 1
【发布时间】:2016-07-19 13:31:41
【问题描述】:

我想做一个 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  |
+------+------------+------------+

【问题讨论】:

  • 请显示预期结果
  • 请参阅第二张表。我想要这个

标签: mysql


【解决方案1】:

你可以使用联合

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;

【讨论】:

  • 让我试试这个。谢谢
  • 显示您的真实数据(示例)..因为查询是正确的..可能是名称不是“video_artist”..但是您有一些空白..更新了 asnwer删除空白的修剪..
  • 感谢您的大力支持。我接受了你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 2019-09-03
  • 2016-03-20
相关资源
最近更新 更多