[英]Combining three SQL statements into one
我已经看过用SQL将两个表合并为一个语句,但是我在网上找到的将3个或更多表合并的示例看起来很复杂。
我希望将3个陈述合并为一个,我相信这是可能的。 这是我想要实现的一些伪代码。
Select tagId from tags where tagName = "test"
->
Select photoId from photoTags where tagId = (tagId from previous statement)
->
Select * from pictures where id = (photoId from previous statement)
如何将其合并为一个陈述? 我对使用JOIN有一个简单的了解,但我不理解多个联接。
作为一个陈述,我正在努力做到的甚至吗?
谢谢
您应该能够使用以下查询访问图片:
SELECT p.*
FROM pictures p
INNER JOIN photoTags pt on pt.photoId = p.id -- Join tables pictures and photoTags
INNER JOIN tags t on pt.tagId = t.tagId -- join tables photoTags and tags
WHERE t.tagName = "test"
它从pictures
表中选择所有列,并按tagName="test"
过滤数据
SELECT T.TagId,PT.PhotoId,P.* FROM Tag T
JOIN photoTags PT On PT.tagId =T.TagId
JOIN pictures P On P.Id=PT.PhotoId
WHERE T.TagName="test"
尝试这个
SELECT p.*
FROM pictures p
JOIN photoTags pt on pt.photoId = p.id
JOIN tags t on pt.tagId = t.tagId
WHERE t.tagName = "test"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.