[英]Combining three SQL statements into one
I have looked at combining two tables into one statement with SQL, but the examples I find online for combining 3 or more look to complicated for me to grasp... 我已经看过用SQL将两个表合并为一个语句,但是我在网上找到的将3个或更多表合并的示例看起来很复杂。
I am looking to combine 3 statements into one, i believe it should be possible. 我希望将3个陈述合并为一个,我相信这是可能的。 Here is some pseudo code for what I am trying to achieve. 这是我想要实现的一些伪代码。
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)
How can I combine this into one statement? 如何将其合并为一个陈述? I have a simple understanding of using JOIN but I don't understand multiple joins. 我对使用JOIN有一个简单的了解,但我不理解多个联接。
Is what I am trying to accomplish even possible as one statement? 作为一个陈述,我正在努力做到的甚至吗?
Thanks 谢谢
You should be able to access the pictures using such query: 您应该能够使用以下查询访问图片:
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"
It selects all columns from pictures
table and filters the data by 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"
Try This 尝试这个
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.