SELECT QUERY FROM三个表SQL

时间:2014-04-10 02:07:39

标签: mysql sql

我的表格如下:

新闻

id_news
id_gallery
headline
content
date

画廊

id_gallery
gallery_name

图片

id_image
id_gallery

我需要为某些新闻选择每张图片。我对sql非常不熟悉,所以我非常感谢各种帮助。我已经搜索过,但是当我尝试使用我发现的查询时,它只是不起作用,我正在做一些非常错误的事情。帮助!

感谢您的时间

6 个答案:

答案 0 :(得分:0)

SELECT i.*
FROM images AS i
JOIN news AS n on n.id_gallery = i.id_gallery
WHERE n.id_news = <selected news>

答案 1 :(得分:0)

SELECT a.*
FROM images a
JOIN news b on b.id_gallery = a.id_gallery

希望这会有所帮助。 :d

答案 2 :(得分:0)

你的问题很模糊,但我需要你需要这样的东西:

select i.id_image
from news n
    left outer join images i on n.id_gallery = i.id_gallery
where n.id_news = 1 -- Or for whichever news id the images are needed

答案 3 :(得分:0)

也许像

select id_news, id_gallery from news join images on images.id_gallary=news.id_gallery

答案 4 :(得分:0)

a) SELECT something : SELECT images.id_image
b) FROM the table images : FROM images
d) Where the id_gallery on news is the same as the id_gallery on images : 
   JOIN news on image.id_gallery = news.id_gallery
e) But not all news, only ceratain news: WHERE news.id_news > 100 (Could use other criteria)

All together
 SELECT images.id_image, images.id_gallery 
 FROM images
 JOIN news on image.id_gallery = news.id_gallery
 WHERE news.id_news > 100

Another way to do it (implicit join) is:
  SELECT images.id_image, images.id_gallery 
 FROM  images,news
 WHERE (image.id_gallery = news.id_gallery) AND (news.id_news > 100)

答案 5 :(得分:0)

如果要根据另一个表中的列从一个表中选择行,则必须使用连接。

因此,我们只想说您正在尝试选择与现有新闻相关联的所有图片。

 select * 
 from images i inner join news n on i.id_gallery = n.id_gallery

这将返回图像可以链接到新闻的所有行上的所有列。