$q = $this->db->query("SELECT user.*, job.*, max(job.End_Time) as END_time
FROM user,job
WHERE user.UID = job.UID and user.UID in( select UID from user where Name LIKE '%" .$search ."%')group by job.UID DESC");
我这样做了,但它显示了第一份工作。
答案 0 :(得分:0)
您需要在作业表上使用另一个自我加入,每个用户的最大作业数为最新作业或用户,当您使用非聚合字段的组具有不确定的结果时,您的子查询也是没有用,除了增加负载,你可以重写你的查询,如下所示
SELECT
u.*,
j.*
FROM
`user` u,
JOIN job j ON u.UID = j.UID
JOIN (SELECT UID,MAX(JID) JID FROM job GROUP BY UID) j1
ON(j.UID = j1.UID AND j.JID = j1.JID)
WHERE u.Name LIKE '%" .$search ."%'