0

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

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

我怎样才能解决这个问题?

4

2 回答 2

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;
于 2013-08-24T20:21:35.083 回答
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
于 2013-08-24T20:23:29.517 回答