【发布时间】:2023-03-24 15:00:01
【问题描述】:
我有 2 个具有不同结果集的 SQL 查询。虽然它们都来自同一个表,但第二个查询确实依赖于另一个表。如何在不使用 UNION 的情况下组合这两个查询?
这是我的查询。
SELECT tmp.id, tmp.title , tmp.description, asst.smallUrl, b1.paramVal as duration, b2.paramVal as clips, asst.fileUrl as video
FROM listDb.baseData tmp
INNER JOIN listDb.tag tag ON tag.baseDataId= tmp.id and tag.tag = 'service app' and tag.status = "active"
INNER JOIN listDb.baseParam b0 ON b0.baseDataId= tmp.id
and ((b0.paramName = "role"
and (b0.paramVal = "public"))
or ((select count(*) from listDb.baseParam temp
where temp.baseDataId= tmp.id and paramName = "role" )=0))
or (b0.paramName = "role" and b0.paramVal = "public" and tmp.owner = 27)
LEFT JOIN listDb.baseParam b1 ON b1.baseDataId= tmp.id and b1.paramName="duration" and b1.status = "active"
LEFT JOIN listDb.baseParam b2 ON b2.baseDataId= tmp.id and b2.paramName=" itemCount" and b2.status = "active"
LEFT JOIN listDb.baseParam b3 ON b3.baseDataId= tmp.id and b3.paramName="previewUrl" and b3.status = "active"
LEFT JOIN assetDb.baseData asst ON asst.id = b3.paramVal and asst.status = "active"
WHERE tmp.status = "active" and tmp.application = "template" and tmp.role = "public"
UNION
SELECT tmp.id, tmp.title , tmp.description, asst.smallUrl, b1.paramVal as duration, b2.paramVal as clips, asst.fileUrl as video
FROM listDb.baseData tmp
INNER JOIN listDb.tag tag ON tag.baseDataId= tmp.id and tag.tag = 'service app' and tag.status = "active"
INNER JOIN listDb.baseParam b0 ON b0.baseDataId= tmp.id
and ((b0.paramName = "role"
and (b0.paramVal = "private" or b0.paramVal = "" and b0.paramVal != "public"))
or ((select count(*) from listDb.baseParam temp
where temp.baseDataId= tmp.id and paramName = "role" )=0))
or (b0.paramName = "role" and b0.paramVal = "public" and tmp.owner = 27)
LEFT JOIN listDb.baseParam b1 ON b1.baseDataId= tmp.id and b1.paramName="duration" and b1.status = "active"
LEFT JOIN listDb.baseParam b2 ON b2.baseDataId= tmp.id and b2.paramName="itemCount" and b2.status = "active"
LEFT JOIN listDb.baseParam b3 ON b3.baseDataId= tmp.id and b3.paramName="previewUrl" and b3.status = "active"
LEFT JOIN assetDb.baseData asst ON asst.id = b3.paramVal and asst.status = "active"
INNER JOIN listDb.checkRestricted cr ON cr.baseDataId= tmp.id and cr.status = "active" and cr.owner = 27
WHERE tmp.status = "active" and tmp.application = "template" and tmp.role = "private"
【问题讨论】:
-
除了这个
(b0.paramVal = "private" or b0.paramVal = "" and b0.paramVal != "public")之外的查询有什么区别吗? -
第二个查询将从另一个表中获取结果.. 在 checkRestricted 表中.. INNER JOIN listDb.checkRestricted cr ON cr.baseDataId= tmp.id and cr.status = "active" and cr.owner = 27