简体   繁体   中英

How to order by a inner join tables

I have 4 tables. here's my code

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

How can i order by this in desc? thanks a lot :)

在此处输入图片说明

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
 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

This would be what you're asking but where does this column come from?

According to your comments, you just need to add the ORDER BY DESC [COLUMN] .

That should fix your issue.

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

I would recommend that you name your variables in a more mnemotechnic way, I will be much easier to analyze, saying SELECT * FROM reservation AS r

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

but make sure

  • there must be the column name reservation_datetime in reservation table
  • guest_id column should be present in reservation table and guest table
  • room_id column must be exist in room table and reservation table
  • room_type_id column must be exist in room table and room_type table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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