改进可怕的多子查询 SQL
Posted
技术标签:
【中文标题】改进可怕的多子查询 SQL【英文标题】:Improve horrible, multiple-sub-query SQL 【发布时间】:2012-01-27 00:20:34 【问题描述】:您将如何改进以下产品 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)
【问题讨论】:
【参考方案1】:类似
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)
【讨论】:
是 on 子句中不必要的括号影响了你。:D 哈!是的,有时我会有点高兴。还是给我点个赞吧。【参考方案2】:试试
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)
【讨论】:
以上是关于改进可怕的多子查询 SQL的主要内容,如果未能解决你的问题,请参考以下文章