在一个查询中提取两个多对多
我有三个表:国家、新闻和关系表country_news。一个国家有很多新闻,新闻也可以来自多个国家。我想从一个国家/地区进行查找以提取所有相关新闻,这很容易:
SELECT news.* FROM country, news, country_news
WHERE country.id = 1
AND country_news.country_id = country.id
AND country_news.news_id = news.id
但现在我还想提取与找到的新闻相关的国家/地区。我没有尝试过多重选择,因为我想尽可能避免它。我尝试了 join 和 group_concat,但无法让它工作。这可以在一个查询中实现吗?
I have three tables: country, news and a relationship table country_news. A country has many news and news can be from many countries. I want to do a look up from a country to extract all the relevant news, which is easy:
SELECT news.* FROM country, news, country_news
WHERE country.id = 1
AND country_news.country_id = country.id
AND country_news.news_id = news.id
But now I would like to extract the countries associated with the found news as well. I haven't tried a multiple select because I would like to avoid it when possible. I tried joins and group_concat, but I could not get it to work. Is this possible in one query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个——
Try this one -
应该为您提供特定 newsid 的所有国家/地区 ID
should give you all country ids for a specific newsid
您还应该选择特定于国家/地区的列。
You should just select country specific columns as well.