简体   繁体   English

在 MySQL 声明方面需要帮助

[英]Need help with a MySQL statement

I have a table of Products that looks like so:我有一个如下所示的Products表:

|    id   |    Description   |   Price    |
|    1    |  dinglehopper    |    2.99    |
|    2    |  flux capacitor  |   48.99    |
|    3    |  thing1          |   48.99    |

And so on...等等...

Then I have an OrderLineItem table which, as you can guess, links each item in an order to the product:然后我有一个OrderLineItem表,您可以猜到,它将订单中的每个项目链接到产品:

|    id   |    productID     |   OrderID  |
|    43   |  1               |    12      |
|    44   |  2               |    12      |
|    52   |  3               |    15      |

So, as you can see, order #12 contains a dinglehopper and flux capacitor.因此,如您所见,订单 #12 包含一个 dinglehopper 和磁通电容器。 How can I get this information in a single query?如何在单个查询中获取此信息? I just want ALL the products associated with a given OrderID in the OrderLineItem table.我只想要 OrderLineItem 表中与给定 OrderID 关联的所有产品。

May be by可能会通过

select p.description,p.id,o.irderId 
from 
    `orderLineItem` o, `product` p 
where 
    p.id = o.productId;

or或者

select p.description,p.id,o.irderId 
from `orderLineItem` o
join  `product` p 
on p.id = o.productId;

@Pete About "single" query part, you should make VIEW from this join, if really going to use a lot. @Pete关于“单一”查询部分,如果真的要使用很多,你应该从这个连接中创建视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM