有另一个问题的mysql查询

时间:2013-08-24 20:14:24

标签: mysql

我正在使用以下查询......

SELECT c.idx, c.club_name, s.sale_event_date, s.club_id, COUNT(*) AS total_guests, 
sum(s.qr_scanned = 1) AS total_scanned_guests, 
SUM(s.rep_sale_commission ON (s.rep_id != 0 AND s.qr_scanned = 1)) AS total_rep_commission, 
 SUM(s.sale_commission) AS total_admin_fees
FROM club c RIGHT JOIN
sales s ON s.club_id = c.idx
WHERE c.admin_id = 37
GROUP BY s.sale_event_date
ORDER BY s.sale_event_date DESC;

但它抛出了一个MySQL错误 - right syntax to use near 'ON (s.rep_id != 0 AND s.qr_scanned = 1)) AS total_rep_commission, SUM(s.' at line 3

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

你的ON关键字应该映射到连接表

所以你使用了错误的查询

 ON (s.rep_id != 0 AND s.qr_scanned = 1) 

可能是在WHERE条件(s.rep_id != 0 AND s.qr_scanned = 1)

SELECT 
  c.idx, c.club_name, s.sale_event_date, s.club_id, COUNT(*) AS total_guests, 
  SUM(s.qr_scanned) AS total_scanned_guests, 
  SUM(s.rep_sale_commission) AS total_rep_commission, 
  SUM(s.sale_commission) AS total_admin_fees
FROM 
  club c 
RIGHT JOIN
  sales s 
ON 
  s.club_id = c.idx
WHERE 
  c.admin_id = 37 AND s.rep_id != 0 AND s.qr_scanned = 1 AND s.qr_scanned = 1
GROUP BY
  s.sale_event_date
ORDER BY
  s.sale_event_date DESC;

答案 1 :(得分:0)

你可能想要这个:

SUM(CASE WHEN s.rep_id != 0 AND s.qr_scanned = 1
         THEN s.rep_sale_commission 
         ELSE 0 END) AS total_rep_commission