学习 SQL - 根据 ItemID 从同一个表中选择详细信息

发布于 2024-12-12 07:14:53 字数 153 浏览 0 评论 0原文

我正在学习SQL。我有一张表,其中包含所有下订单的详细信息。我需要找到包含特定商品的所有订单。所以我需要选择 ItemID = 102011 来获取订单号,然后他们按 OrderId 选择包含该商品的所有记录。我不确定如何实现这一点。

一切都在一张表中。

谢谢

I am in the process of learning SQL. I have a table that has the details of all orders placed. I need to locate all order that contain a specific item. SO i need to select the ItemID = 102011 to get the order numbers and them select all of the records by OrderId that have that item. I am not sure exactly how to make this happen.

Everything is in one table.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绅士风度i 2024-12-19 07:14:54

不确定你所说的所有内容都在一张表中是什么意思。可以贴一下表结构吗?
在一个分支中,这就是您正在寻找的:

SELECT * FROM <orders_table> 
WHERE order_id IN (SELECT * FROM <orders_table> WHERE ItemID = 102011)

或者这个

SELECT * FROM <orders_table> WHERE ItemID = 102011 ORDER BY OrderId

Not sure what you mean by everything is in one table. Can you post the table structures?
In an offshot that this is what you are looking for:

SELECT * FROM <orders_table> 
WHERE order_id IN (SELECT * FROM <orders_table> WHERE ItemID = 102011)

or this

SELECT * FROM <orders_table> WHERE ItemID = 102011 ORDER BY OrderId
云之铃。 2024-12-19 07:14:54

这可能取决于您的 SQL 风格,但在 SQL Server 中,类似以下内容可以正常工作:

SELECT * FROM OrdersItems WHERE OrderID IN (SELECT OrderID FROM OrderItems WHERE ItemId = 102011)

This will likely depend on your flavour of SQL, but in SQL Server something like the following will work fine:

SELECT * FROM OrdersItems WHERE OrderID IN (SELECT OrderID FROM OrderItems WHERE ItemId = 102011)
给不了的爱 2024-12-19 07:14:53

尝试这样的事情(假设你的表结构有一定数量):

select *
from yourOrdersTable
where ItemID = 102011
order by OrderId

Try something like this (assuming a certain amount of your table structure):

select *
from yourOrdersTable
where ItemID = 102011
order by OrderId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文