改进可怕的多子查询SQL

时间:2012-01-27 00:20:34

标签: mysql query-optimization subquery

您如何改进以下产品i18n检索SQL以不使用子查询?

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)

2 个答案:

答案 0 :(得分:2)

类似

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)

答案 1 :(得分:0)

尝试

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)