简体   繁体   English

有条件的左联接中的查询问题

[英]Query Issue in left join with a condition

I have this query which helps me to get the points for a current user, now, i have a problem on setting a condition... 我有这个查询,可以帮助我获取当前用户的积分,现在,我在设置条件时遇到问题...

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

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

I need to find the points for this specific user.. Am i doing something wrong with it? 我需要为该特定用户找到要点。.我做错了吗? I mean, i know i am, but what? 我的意思是,我知道我是,但是呢? Thanks 谢谢

write your query like this 这样写你的查询

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM