给出此数据集:
+----+-----------+----------+
| ID | ProductID | quantity |
+----+-----------+----------+
| 1 | 1 | 4 |
| 1 | 2 | 13 |
| 1 | 4 | 12 |
| 1 | 19 | 3 |
| 2 | 19 | 4 |
| 2 | 22 | 2 |
| 2 | 2 | 6 |
| 2 | 38 | 1 |
| 2 | 14 | 4 |
| 3 | 11 | 5 |
| 3 | 12 | 6 |
| 4 | 13 | 3 |
| 4 | 14 | 11 |
| 5 | 15 | 2 |
| 6 | 16 | 3 |
| 7 | 17 | 4 |
| 8 | 18 | 9 |
| 8 | 19 | 8 |
+----+-----------+----------+
我的结果集应如下所示:
+----+-----------+----------+
| ID | ProductID | quantity |
+----+-----------+----------+
| 1 | 1 | 4 |
| 1 | 2 | 13 |
| 1 | 4 | 12 |
| 1 | 19 | 3 |
| 2 | 19 | 4 |
| 2 | 22 | 2 |
| 2 | 2 | 6 |
| 2 | 38 | 1 |
| 2 | 14 | 4 |
| 8 | 18 | 9 |
| 8 | 19 | 8 |
+----+-----------+----------+
我正在尝试查找ProductID 19,结果集必须包含所有ID,产品ID和数量。
这是如何工作的?我尝试了不同类型的子查询和JOINS,但是我无法处理。
感谢您的任何帮助!
编辑:我正在使用SQL Server 2018
数据架构: Data
答案 0 :(得分:1)
似乎您只需要in
select t.ID ,t.ProductID,t.quantity
from your_table t where t.ID in ( select ID from your_table t1 where t1.PRODUCT_ID =19)
从您的评论看来,您似乎需要使用in的子查询
检查此小提琴链接
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=dfd72f991002af383465af763af290c3
答案 1 :(得分:1)
SELECT * FROM TABLENAME
WHERE ID IN (SELECT ID FROM TABLENAME WHERE PRODUCT_ID = <yourID>)
我想这就是您要寻找的
根据您的图片(考虑到gtin是产品ID)
您的查询应为
SELECT * FROM TABLENAME
WHERE receipt_header_id IN (SELECT receipt_header_id
FROM TABLENAME
WHERE gtin = <yourID>)