我在这里查了好几个帖子,找不到我问题的答案.... 我试图用多个表做一个mysql查询,然后离开加入最后一个表。它不喜欢左连接,当我包含它时没有给我任何结果。非常感谢任何帮助:)
(我正在使用Joomla)
query = "SELECT DISTINCT u.id as uid, CONCAT(u.first_name,' ',u.last_name) as name1,
u.grad as grad, u.opt_out as opt_out
FROM #__bl_teams as t, #__bl_regions as r, #__users as u
LEFT JOIN #__bl_paid as pd ON pd.u_id = u.id
WHERE u.team_id = t.id AND u.team_id != '' AND u.s_id = $sid
AND ((t.id = $tid)OR($tid=0)) AND (t.id IN ($teamsfull))
AND ( (t.id IN(".$tc_teams."))OR(".$tc_id." = 0))
AND ((r.id = ".$mid.")OR(".$mid." = 0))
AND ((r.s_id = ".$sid.")OR(".$mid." = 0))
AND ( (FIND_IN_SET(t.id,r.teams) )OR(".$mid." = 0) )
AND u.id NOT IN($paidrows) AND u.id NOT IN ($rsrows) GROUP BY u.id";
$db->setQuery($query, $pageNav->limitstart, $pageNav->limit);
$rows50 = $db->loadObjectList();
答案 0 :(得分:0)
您正在混合旧式连接和新式连接。那是你的问题。在from
语句中,表格别名无法以您期望的方式识别。埋在documentation中的是这个警告:
但是,逗号运算符的优先级小于INNER JOIN,CROSS JOIN,LEFT JOIN等等。如果你混合使用逗号连接 当有连接条件时,其他连接类型,错误 表格未知栏' col_name'在' on条款'可能导致。信息 关于这个问题的处理将在本节后面给出。
简单规则:永远不要在from
子句中使用逗号。
因此,使用显式连接重写您的查询,这将解决问题。
答案 1 :(得分:-1)
我不知道实际的表,但是这个查询太长并且会产生意外的结果。无论如何,Left也加入了其他表。
query = "SELECT DISTINCT u.id as uid, CONCAT(u.first_name,' ',u.last_name) as name1,
u.grad as grad, u.opt_out as opt_out
FROM #__users as u
LEFT JOIN #__bl_paid as pd ON pd.u_id = u.id
LEFT JOIN #__bl_teams as t ON t.u_id = u.id
LEFT JOIN #__bl_regions as r ON r.u_id = u.id
WHERE u.team_id != ''
AND u.s_id = $sid
AND ((t.id = $tid)
OR($tid=0))
AND (t.id IN ($teamsfull))
AND ( (t.id IN(".$tc_teams."))
OR(".$tc_id." = 0))
AND ((r.id = ".$mid.")
OR(".$mid." = 0))
AND ((r.s_id = ".$sid.")
OR(".$mid." = 0))
AND ( (FIND_IN_SET(t.id,r.teams) )
OR(".$mid." = 0) )
AND u.id NOT IN($paidrows)
AND u.id NOT IN ($rsrows)
GROUP BY u.id";