Joseph Woodward
Joseph Woodward

Reputation: 9281

Ordering joined data

I'm currently performing the following query across 3 tables; handset, deal and tariff:

SELECT
    h.*, d.*, t.* FROM handset h
LEFT JOIN deal d ON h.id = d.handset_id
LEFT JOIN tariff t ON d.tariff_id = t.id
ORDER BY
    h.popularity
DESC LIMIT 10

The Tariff table has a column called price and I would like to join the tariff with the lowest price onto the results queried.

Upvotes: 0

Views: 28

Answers (1)

Rohit R.K.
Rohit R.K.

Reputation: 297

   SELECT
   h.*, d.*, t.* FROM handset h
   LEFT JOIN deal d ON h.id = d.handset_id
   LEFT JOIN tariff t ON d.tariff_id = t.id
   where t.price=(select min(price) from tariff)
   ORDER BY
   h.popularity
   DESC LIMIT 10

Upvotes: 1

Related Questions