SELECT
*
FROM
`table_1`
WHERE
`id` =
(
SELECT
`an_id`
FROM
`table_2`
WHERE
`id` =
(
SELECT
`another_id`
FROM
`table_3`
WHERE
`id` =
(
SELECT
`yet_another_id`
FROM
`table_4`
WHERE
`id` = 123
)
)
)
非常难看对吗? 我猜我可以在这里使用JOIN - 但是怎么样?
答案 0 :(得分:3)
select t1.*
from
table1 t1,
table2 t2,
table3 t3,
table4 t4
where
t4.id = 123
and t3.id = t4.yet_another_id
and t2.id = t3.another_id
and t1.id = an_id;
答案 1 :(得分:2)
这会对您的数据返回相同的结果吗?:
SELECT
table_1.*
FROM
table_1
INNER JOIN table_2
ON table_1.id = table_2.an_id
INNER JOIN table_3
ON table_2.id = table_3.another_id
INNER JOIN table_4
ON table_3.id = table_4.tet_another_id
WHERE
table_4.id = 123
即使它确实如此,仍然值得对两者进行基准测试,看看哪个实际上更快。
答案 2 :(得分:1)
试试这个:
SELECT
t1.*
FROM
table_1 AS t1
JOIN
table_2 AS t2 ON (t1.id = t2.id)
JOIN
table_3 AS t3 ON (t2.id = t3.id)
JOIN
table_4 AS t4 ON (t3.id = t4.id)
WHERE
t4.id = 123
也许这有助于你......