SELECT a.id,
a.username,
a.email,
a.created,
b.user_id,
SUM(b.price) +
SUM(IF(d.beginner =0, 15, 0)) + CASE WHEN SUM(b.price) >499 THEN 100
ELSE 0 END AS points,
SUM(b.price) AS pris,
b.created,
c.user_id,
c.referrer_id,
c.created,
d.id,
d.beginner,
d.winner_id
FROM users a,
accounts b,
referrals c,
product d
WHERE a.created BETWEEN '2013-04-01' AND '2013-06-30'
AND d.created BETWEEN '2013-06-01' AND '2013-06-30'
AND b.created BETWEEN '2013-06-01' AND '2013-06-30'
AND a.id = b.user_id
AND a.id = c.user_id
AND d.winner_id = a.id
GROUP BY c.referrer_id
ORDER BY `points` DESC LIMIT 0 , 3
在这个查询中,我有四个表。对于product
表格,我希望它也可以根据created
. 但是当我喜欢我在查询中所做的事情时,它并没有给我正确的结果。我也想用这个查询选择产品表,条件是我只需要选择从 6 月 1 日到 6 月 30 日的记录,并根据如果在产品表中找到获胜者将增加 15 分的条件放置它们。
如果您看到此SUM( IF( d.beginner =0, 15, 0 ) )
条件,如果我不添加AND d.created
BETWEEN '2013-06-01'
AND '2013-06-30'
并且如果用户未在产品表中找到但在其余两个条件中具有例如 30 点之类的点,则它不会在此查询中显示。我不知道为什么?我希望在 6 月 1 日至 6 月 30 日期间选择产品和帐户表,并在 4 月 1 日至 6 月 30 日期间选择表用户
SELECT a.id, a.username, a.email, a.created, b.user_id, SUM( b.price ) +
CASE WHEN SUM( b.price ) >499
THEN 100
ELSE 0
END AS gr, SUM( b.price ) AS pris, b.created, c.user_id, c.referrer_id, c.created
FROM users a, accounts b, referrals c
WHERE a.created
BETWEEN '2013-04-01'
AND '2013-06-30'
AND b.created
BETWEEN '2013-06-01'
AND '2013-06-30'
AND a.id = b.user_id
AND a.id = c.user_id
GROUP BY c.referrer_id
ORDER BY `gr` DESC
LIMIT 0 , 3
我现在已经从这里删除了产品表条件。所以现在很容易理解。我想添加表products
,其中仅从 6 月 1 日到 30 日选择行,第二个条件是如果用户在winner_id
列中的产品表中找到,则添加 15 分。