使用条件查询左连接中的问题

时间:2012-11-16 09:40:33

标签: php mysql

我有这个查询可以帮助我获得当前用户的积分,现在,我在设置条件时遇到了问题......

$query4 =  'SELECT u.*, SUM(c.ts) AS total_sum1, SUM(r.bv) AS total_sum 
FROM users u 
LEFT JOIN 
 (SELECT user_id ,SUM(points) AS ts FROM coupon GROUP BY user_id) c 
 ON u.user_id=c.user_id AND u.user_id="'.$_SESSION['user_name'].'"
LEFT JOIN 
 (SELECT user_id ,SUM(basket_value) AS bv FROM retailer GROUP BY user_id) r 
ON u.user_id=r.user_id 
GROUP BY u.user_id';

我的问题是AND u.user_id="'.$_SESSION['user_name'].'"

我需要找到这个特定用户的要点.. 我做错了吗?我的意思是,我知道我,但是什么? 感谢

1 个答案:

答案 0 :(得分:1)

像这样编写你的查询

$query4 =  'SELECT u.*, SUM(c.ts) AS total_sum1, SUM(r.bv) AS total_sum 
FROM users u 
LEFT JOIN 
 (SELECT user_id ,SUM(points) AS ts FROM coupon GROUP BY user_id) c 
 ON u.user_id=c.user_id 
LEFT JOIN 
 (SELECT user_id ,SUM(basket_value) AS bv FROM retailer GROUP BY user_id) r 
ON u.user_id=r.user_id 
where u.user_id="'.$_SESSION['user_name'].'"
GROUP BY u.user_id';