php - 涉及4张表的查询,inner join?

标签 php mysql sql

我正在开发一个 PHP 应用程序,我需要一个涉及 4 个表的查询。表格是这样的:

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

我想显示按州名过滤的结果(或者我认为相同的 state_pk)。 我想显示某个州的新闻。

我不经常使用 MySQL,但在这个应用程序中我做了几个这样的查询,我不确定 inner join 是否是一个好的解决方案。

要仅显示特定州的新闻,哪个查询最有效?

最佳答案

您应该使用以下查询来连接表:

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");

关于php - 涉及4张表的查询,inner join?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22628687/

相关文章:

python - 此 python 代码中此 POST 方法的 json 请求是否正确?

MySQL 错误 #1415 - 不允许从函数返回结果集

php - 在 PHP 和 JavaScript 中导出 csv 文件

php - 从样式 css 获取背景图像 URL 的简单 html dom

php - 将变量放置在 div id 中

php - 将 Excel 上传到 MySql 数据库时排除行

php - Yii + mysql + cloudControlled

php - 使用数据库而不是配置文件来存储配置

mysql - 使用 mysql 在 Codeigniter 中获取按特定列排序的行

sql - SQL Server 中的子查询求和