MSSQL 选择“垂直”-where
除了“垂直位置”之外,我真的不知道如何解释。
想象一下下表:
TAGID|PRODUCTID|SHOP_ID
59 |3418-7 |38
61 |3418-7 |38
60 |4227-4 |38
61 |4227-4 |38
现在我想返回与标签 ID:59,61 相关的所有产品 ID。换句话说,两个标签 ID 都存在行的产品 ID 值。
所以我想返回 3418-7,但不是 4227-4
如何在 SQL 语句中尽可能简单地编写它?
这是我到目前为止的工作声明,但我觉得这可以通过更聪明的方式来完成:
SELECT
productid
FROM shop_tag_relations
WHERE
productid IN (select productid from shop_tag_relations WHERE tagid=59)
AND
productid IN (select productid from shop_tag_relations WHERE tagid=61)
GROUP BY productid,shop_id
I don't really know how to explain except with "vertical where".
Imagine the following table:
TAGID|PRODUCTID|SHOP_ID
59 |3418-7 |38
61 |3418-7 |38
60 |4227-4 |38
61 |4227-4 |38
Now I want to return all product IDs, that have relation to the tag IDs: 59,61. In other words, values of product ID where rows exist for both tag IDs.
So I want to return 3418-7, but not 4227-4
How do I write this as simple as possible in a SQL statement?
This is the working statement I have so far, but I feel this could be done in a much smarter way:
SELECT
productid
FROM shop_tag_relations
WHERE
productid IN (select productid from shop_tag_relations WHERE tagid=59)
AND
productid IN (select productid from shop_tag_relations WHERE tagid=61)
GROUP BY productid,shop_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者
Or