Paul
Paul

Reputation: 21

MySQL One to Many Join?

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?

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

Upvotes: 1

Views: 488

Answers (1)

ACdev
ACdev

Reputation: 62

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

Upvotes: 1

Related Questions