请在此处使用此查询,以便从数据库中提取一些行。
$rs = mysql_query("SELECT u.*, SUM(c.ts) AS total_sum1, SUM(m.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 like'%$search%'
LEFT JOIN
(SELECT user_id ,SUM(points) AS bv FROM matching GROUP BY user_id) m
ON u.user_id=m.user_id
GROUP BY u.user_id limit $offset,$rows");
我需要对其进行修改,以便只查询满足此条件的行:
u.user_id like '%$search%'
我应该把上述条件放在哪里? 我尝试添加AND运算符但它仍然不会给出一行,因为user_id彼此不同。
由于
答案 0 :(得分:0)
您需要添加WHERE
子句才能添加该条件:
$rs = mysql_query("SELECT u.*, SUM(c.ts) AS total_sum1, SUM(m.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 like'%$search%'
LEFT JOIN
(SELECT user_id ,SUM(points) AS bv FROM matching GROUP BY user_id) m
ON u.user_id=m.user_id
WHERE u.user_id like '%$search%'
GROUP BY u.user_id limit $offset,$rows");
附注:
mysql_*
库,请考虑升级到现代API,例如PDO或MySQLi 答案 1 :(得分:0)
你可以尝试这个SQL。(我不知道PHP)
SELECT
u.*,
SUM(c.points) AS total_sum1,
SUM(m.points) AS total_sum
FROM users u
INNER JOIN coupon c ON u.user_id=m.user_id
INNER JOIN matching m ON u.user_id=m.user_id
WHERE u.user_id like '%$search%'
GROUP BY u.user_id limit $offset,$rows" ;