简体   繁体   中英

Order a table by another table column?

I have this query:

$q = "SELECT * FROM user 
WHERE sec='1' AND reg_by='".$_SESSION['login_username']."' 
ORDER BY date DESC LIMIT $startrow, 30 ";

I have another table which stores appointments, it has a column named meet.

How can I sort this query by meet?

Not all data at users are in other table.

你可以像这样使用加入

select user.*,meet.* from user left join meet on (meet.userid = user.id) where user.sec='1' AND user.reg_by='".$_SESSION['login_username']."' order by meet.userid DESC

You can use the below query. Replace another_table with your original table name :

$q = "SELECT u.* FROM user AS u LEFT JOIN another_table AS at ON u.userid = at.userid WHERE u.sec='1' AND u.reg_by='".$_SESSION['login_username']."' ORDER BY at.meet DESC LIMIT $startrow, 30 ";
$q = "SELECT * FROM user INNER JOIN user
ON meets.userid=user.userid WHERE sec='1' AND reg_by='".$_SESSION['login_username']."'  ORDER BY date DESC LIMIT $startrow, 30 ";

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