如何通过内部联接表进行排序

时间:2017-10-05 13:57:11

标签: php mysql

我有4张桌子。这是我的代码

SELECT * FROM reservation AS t1 INNER JOIN guest AS t2
ON t1.guest_id = t2.guest_id 
INNER JOIN room AS t3 
ON t1.room_id = t3.room_id 
INNER JOIN room_type AS t4
ON t3.room_type_id = t4.room_type_id

我如何在desc中订购?非常感谢:))

enter image description here

4 个答案:

答案 0 :(得分:0)

SELECT * FROM reservation AS t1 INNER JOIN guest AS t2
ON t1.guest_id = t2.guest_id 
INNER JOIN room AS t3 
ON t1.room_id = t3.room_id 
INNER JOIN room_type AS t4
ON t3.room_type_id = t4.room_type_id 
order by t1.guest_id desc

答案 1 :(得分:0)

 SELECT * FROM reservation AS t1 INNER JOIN guest AS t2
 ON t1.guest_id = t2.guest_id 
 INNER JOIN room AS t3 
 ON t1.room_id = t3.room_id 
 INNER JOIN room_type AS t4
 ON t3.room_type_id = t4.room_type_id
 ORDER BY t1.reservation_datetime DESC

这就是你要问的问题,但这个专栏的来源是什么?

答案 2 :(得分:0)

根据您的评论,您只需添加ORDER BY DESC [COLUMN]

这应该可以解决你的问题。

SELECT * FROM reservation AS t1
INNER JOIN guest AS t2
ON t1.guest_id = t2.guest_id 
INNER JOIN room AS t3 
ON t1.room_id = t3.room_id 
INNER JOIN room_type AS t4
ON t3.room_type_id = t4.room_type_id
ORDER BY t1.reservation_datetime DESC

我建议你用更多的mnemotechnic方式命名你的变量,我会更容易分析,说SELECT * FROM reservation AS r

答案 3 :(得分:0)

SELECT * FROM reservation AS t1 
INNER JOIN guest AS t2 ON t1.guest_id = t2.guest_id 
INNER JOIN room AS t3 ON t1.room_id = t3.room_id 
INNER JOIN room_type AS t4 ON t3.room_type_id = t4.room_type_id
ORDER BY t1.reservation_datetime DESC

但请确保

  • 预订表中必须有列名称reservation_datetime
  • guest_id列应出现在预订表和来宾表中
  • room_id列必须存在于房间表和预订表中
  • room_type_id列必须存在于room table和room_type表中