简体   繁体   English

查询涉及4个表,内部联接?

[英]Query which involves 4 tables, inner join?

I am just developing a PHP aplication, and I need a query which involves 4 tables. 我只是在开发PHP应用程序,我需要一个涉及4个表的查询。 tables are like: 表格就像:

news {news_pk, user, description, building_pk}
building {building_pk, area_pk, description}
area {area_pk,state_pk}
states {state_pk, name}

I would like to show results filtered by state name (or what I think is the same state_pk ). 我想显示按州名(或我认为相同的state_pk )过滤的结果。 I would like to show news from a certain state. 我想显示某个状态的新闻。

I am not using MySQL so often, but in this app I am doing several queries like this and I am not sure if the inner join is a good solution. 我不是经常使用MySQL,但是在这个应用程序中,我正在执行类似这样的几个查询,并且不确定内部联接是否是一个好的解决方案。

Which query could be the most effective to show only news from a certain state? 哪个查询最有效地仅显示来自特定状态的新闻?

It very much depends on what you want to do, inner join might be a good solution if it is what you are looking for. 这在很大程度上取决于您要执行的操作,如果您正在寻找内部联接,则它可能是一个很好的解决方案。 You can seel the difference between joins in this answer: What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? 您可以在此答案中看到联接之间的区别: INNER JOIN,LEFT JOIN,RIGHT JOIN和FULL JOIN有什么区别?

Hope it helps! 希望能帮助到你! :) :)

您应该对表的联接使用以下查询:

mysql_query("select a.news_pk,a.user.a.description,a.buiding_pk,b.building_pk,b.area_pk,b.description,c.area_pk,c.state_pk,d.state_pk,d.name from news as a,buiding as b,area as c,states as d where a.buiding_pk=b.building_pk and b.area_pk=c.area_pk and c.state_pk=d.state_pk");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM