简体   繁体   English

如何使用外键从另一个表的顺序中获取记录

[英]How do you fetch records from a table with order of another table using foreign key

I have two tables in my SQL, 'post' and 'business' The columns in the post are:我的 SQL 中有两个表,'post' 和 'business' 帖子中的列是:

  • Post_ID Post_ID
  • Content内容
  • Img图像
  • Business_ID业务_ID

The columns in business are业务中的列是

  • Business_ID业务_ID
  • Business name企业名称
  • Business rating商业评级

What I want is I want the posts which are posted by businesses with high business rating come first before others, please is there a way to fetch the posts with order of business rating???我想要的是我希望商业评级高的企业发布的帖子在其他人之前,请问有没有办法按照商业评级的顺序获取帖子?

To order results of posts with associated business rating you need a join query要订购具有相关业务评级的帖子结果,您需要一个连接查询

select p.*
from posts as p
join business as b on b.business_id = p.business_id
order by b.business_rating desc

order by查询排列评级使用order by

select * from posts,business order by business_rating where posts.business_ID=business.business_ID;

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

相关问题 如何使用 CodeIgniter 3 中的外键从表中获取列数据 - How to fetch columns data from a table using the foreign key in CodeIgniter 3 如何在laravel中使用外键参考单个表获取两个记录 - how to fetch two records with reference to single table using foreign key in laravel 在另一个表的主键;外键列中插入记录 - Inserting records in a primary;foreign key columns from another table Codeigniter:如何使用外键ID从外表获取不同的列数据? - Codeigniter : How to fetch different column data from foreign table using foreign key id? SQL-显示表中的记录,其中外键值与另一个表中的主键值匹配 - SQL - Displaying records from a table where the foreign key value matches the primary key value in another table 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another 如何将主键作为外键插入另一个表? - How do I insert the primary key to another table as foreign key? 如何从具有不同外键名称的两个表中获取数据 - how to fetch data from the two table with different foreign key names 如何根据外键从表中获取多个值? - How to fetch multiple values from the table based on foreign key? 如何在zf2中使用外键从表中获取数据? - How to fetch data from table with foreign key in zf2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM