在编写以下查询时,它会抱怨" so.id"专栏不详!以下查询在MS SQL中工作正常但在MySQL中没有?如何在MySQL中编写嵌套的select语句?
它的等价物是什么?
select so.*
from smart_objects as so
inner join (
SELECT st.objectid, st.issueid
FROM smart_targets as st
WHERE st.issueid != 0
AND
( (st.objectid = so.id)
OR
(
st.objectid in (
SELECT sor.parent
FROM smart_objectrelations as sor
WHERE sor.child = so.id)
)
)) as soi
on soi.objectid = so.id
where so.id < 100;
我基本上需要内部加入嵌套的Select SQL。我的select语句比上面的更复杂,上面只是解释嵌套表的简化版本。
我还尝试了另一个建议:
select so.*
from smart_objects as so
inner join (
SELECT st.objectid, st.issueid
FROM smart_targets as st, smart_objects as so
WHERE st.issueid != 0
AND
( (st.objectid = so.id)
OR
(
st.objectid in (
SELECT sor.parent
FROM smart_objectrelations as sor
WHERE sor.child = so.id)
)
)) as soi
on soi.objectid = so.id
where so.id < 100;
上面的代码进入一个永无止境的循环,而我只想让嵌套的sql为父代的每一行运行。
更新答案:
如何简化以下查询?
select so.*
from smart_objects as so
inner join (
SELECT st.objectid, st.issueid
FROM smart_targets as st, smart_objects as so
WHERE st.objectid = so.id
and st.issueid != 0
AND
( (st.objectid = so.id)
OR
(
st.objectid in (
SELECT sor.parent
FROM smart_objectrelations as sor, smart_objects as so
WHERE sor.child = so.id)
)
)
) as soi
on soi.objectid = so.id
答案 0 :(得分:1)
试试这个:我想这应该有效
select so.*
from smart_objects as so
inner join (SELECT st.objectid, st.issueid FROM smart_targets as st,smart_objects as so
WHERE st.objectid = so.id) as soi
on soi.objectid = so.id