开发者

MySQL One to Many Join?

开发者 https://www.devze.com 2023-02-18 13:56 出处:网络
I have 2 tables. 1 Table has pricing categories. pricing :: id, pricing_name The second table has pricing options

I have 2 tables. 1 Table has pricing categories.

pricing :: id, pricing_name

The second table has pricing options

pricing_options :: id, parent, name, value

How do I select all of the values in the pricing table, then select all of pricing options so I would get an output like this?

开发者_Python百科

id [1], pricing_name [some name], pricing_options [ array containing options]


What do you mean with [array containing options]?

Supposing pricing_options.parent is a foreign key to pricing.id, you can try with:

SELECT id, pricing_name, name, value FROM pricing LEFT JOIN pricing_options ON pricing .id = pricing_options.parent;

This will give you all the pricings with they related pricing_options. You will have nulls on pricing_options part if there is no pricing_option for that pricing.

Hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消