请注意,我正在为我的主查询和子查询调用并加入相同的表。
现在我的实际查询正在使用更多这样的子查询。
有没有办法从主查询中调用子查询字段,从而无需在子查询中重用相同的连接表?我想在不牺牲速度的情况下提高效率。
SELECT tb1.id, tb1.title,
(SELECT tb1.title
FROM table1 AS tb1
JOIN table2 AS tb2 ON tb2.id = tb1.id
LEFT JOIN table3 AS tb3 ON tb2.id = tb3.id
WHERE tb1.id > '123' LIMIT 1) AS next
FROM table1 AS tb1
JOIN table2 AS tb2 ON tb2.id = tb1.id
LEFT JOIN table3 AS tb3 ON tb2.id = tb3.id
WHERE tb1.id='123'
答案 0 :(得分:3)
您可以使用View来抽象该查询。
CREATE VIEW tbl_view AS
SELECT
tb1.id as id,
tb1.title as title
FROM table1 AS tb1
JOIN table2 AS tb2 ON tb2.id = tb1.id
LEFT JOIN table3 AS tb3 ON tb2.id = tb3.id
较短的查询将是
SELECT
id,
title,
(SELECT title FROM tbl_view
WHERE id > '123' LIMIT 1) AS next
FROM
tbl_view
WHERE id='123'