按另一个表列排序表?

时间:2017-02-14 12:48:01

标签: php mysql

我有这个问题:

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

我有另一个存储约会的表,它有一个名为meet的列。

如何通过meet?

对此查询进行排序

并非用户的所有数据都在其他表中。

3 个答案:

答案 0 :(得分:0)

您可以像

一样使用加入
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

答案 1 :(得分:0)

您可以使用以下查询。将another_table替换为原始表名:

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

答案 2 :(得分:0)

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