简体   繁体   中英

Improve horrible, multiple-sub-query SQL

How would you improve the following product i18n retrieval SQL to NOT use sub-queries?

SELECT
 l.code,
 l.rtl,
 l.name,
 (SELECT title FROM product_i18n WHERE product_id = :product_id AND culture = l.code) AS title,
 (SELECT description FROM product_i18n WHERE product_id = :product_id AND culture = l.code) AS description,
 (SELECT keywords FROM product_i18n WHERE product_id = :product_id AND culture = l.code) AS keywords
FROM
 language AS l
WHERE
 l.status = 1
ORDER BY
 IFNULL(l.sort, l.id)

something like

SELECT
 l.code,
 l.rtl,
 l.name,
 p.title,
 p.description,
 p.keywords
From
 language AS l
inner join product_il8n p on p.culture = l.code and p.product_id = :product_id
WHERE
 l.status = 1
ORDER BY
 IFNULL(l.sort, l.id)

Try

SELECT
 l.code,
 l.rtl,
 l.name,
 p.title,
 p.description,
 p.keywords
FROM
 language AS l
INNER JOIN product_i18n p ON (p.product_id = :product_id AND p.culture = l.code)
WHERE
 l.status = 1
ORDER BY
 IFNULL(l.sort, l.id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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