我已經寫了一個MySQL查詢(廣告旋轉算法)從多個表中獲取記錄。加入過濾器不能按預期在MySQL中工作
select Q2.*
from User u,(
select
a.adId as "AdId",
a.UserId as "UserId",
a.Title as "Title",
a.ImageURL as "ImageURL",
a.RefURL as "RefURL",
a.CreateDate as "CreateDate",
a.StartDate as "StartDate",
a.RunTill as "RunTill",
a.Budget as "Budget",
a.Status as "Status",
(a.budget - COALESCE(Q1.A2,0)) as "Remaining"
from Ad a
LEFT JOIN(
select
AdId as A1,
count(*) as A2
from Referral
where date(ReferralDate)=date(CURRENT_TIMESTAMP)
group by AdId
) as Q1
ON a.AdId = Q1.A1
and a.StartDate<CURRENT_TIMESTAMP
and a.RunTill>CURRENT_TIMESTAMP
and a.Status = 1
) as Q2
where u.Authorized = true
and u.Balance>1
and u.UserId = Q2.UserId
order by Q2.Remaining desc;
上面的查詢有一個過濾器a.Status = 1
,但結果是,我越來越有狀態行!= 1爲好。結果集如下:
+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------
------------+---------------------+---------------------+--------+--------+-----------+
| AdId | UserId | Title | ImageURL | RefURL | CreateDa
te | StartDate | RunTill | Budget | Status | Remaining |
+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------
------------+---------------------+---------------------+--------+--------+-----------+
| 382944516829 | 724511865288 | Online Advertising for Nepal | /static/image/adimage/noimage.jpg | http://www.nepaladz.com | 2011-11-
03 13:47:47 | 2011-11-03 00:00:00 | 2011-11-30 00:00:00 | 100 | 0 | 100 |
| 973252821643 | 724511865288 | Models, news, fashion and more | http://nepalads.servehttp.com:8080/static/image/adimage/7.jpg | http://www.cybersansar.com | 2011-10-
18 15:57:49 | 2011-11-03 10:59:57 | 2011-11-18 15:57:49 | 70 | 1 | 70 |
| 805400799468 | 724511865288 | Alibaba market | http://nepalads.servehttp.com:8080/static/image/adimage/4.jpg | http://www.alibaba.com | 2011-10-
18 15:54:42 | 2011-11-03 10:59:57 | 2011-11-18 15:54:42 | 60 | 1 | 60 |
| 333179565363 | 724511865288 | Nepal AT&T Network | http://nepalads.servehttp.com:8080/static/image/adimage/3.jpg | http://www.att.com | 2011-10-
18 15:54:00 | 2011-11-03 10:59:57 | 2011-11-18 15:54:00 | 60 | 1 | 60 |
| 576540783739 | 724511865288 | Travel with us! | http://nepalads.servehttp.com:8080/static/image/adimage/8.jpg | http://www.manang.com | 2011-10-
18 15:58:39 | 2011-11-03 10:59:57 | 2011-11-18 15:58:39 | 45 | 1 | 43 |
| 011731192504 | 724511865288 | Nepal Online Shopping | http://nepalads.servehttp.com:8080/static/image/adimage/11.jpg | http://www.harilo.com | 2011-10-
18 16:02:32 | 2011-11-03 10:59:57 | 2011-11-18 16:02:32 | 45 | 1 | 42 |
| 232044045570 | 724511865288 | Himalayan Java | http://nepalads.servehttp.com:8080/static/image/adimage/1.jpg | http://www.himalayanjava.com | 2011-10-
18 15:51:34 | 2011-11-03 10:59:57 | 2011-11-18 15:51:34 | 30 | 1 | 30 |
| 471978035014 | 724511865288 | Home TV. 50% discount | http://nepalads.servehttp.com:8080/static/image/adimage/5.jpg | http://www.dishhome.com.np | 2011-10-
18 15:56:03 | 2011-11-03 10:59:57 | 2011-11-18 15:56:03 | 30 | 1 | 30 |
| 543726500808 | 724511865288 | Live the adventure | http://nepalads.servehttp.com:8080/static/image/adimage/9.jpg | http://www.adventuresnepal.com | 2011-10-
18 15:59:21 | 2011-11-03 10:59:57 | 2011-11-18 15:59:21 | 25 | 1 | 25 |
| 757765466809 | 724511865288 | Wanna meet me? Click here | http://nepalads.servehttp.com:8080/static/image/adimage/10.jpg | http://www.missnepal.com.np | 2011-10-
18 16:00:14 | 2011-11-03 10:59:57 | 2011-11-18 16:00:14 | 25 | 1 | 23 |
| 890639256469 | 724511865288 | Learn dance from Gurus | http://nepalads.servehttp.com:8080/static/image/adimage/6.jpg | http://www.salsanepal.com | 2011-10-
18 15:56:45 | 2011-11-03 10:59:57 | 2011-11-18 15:56:45 | 15 | 1 | 15 |
| 838481835983 | 724511865288 | Fashionista Nepal | http://nepalads.servehttp.com:8080/static/image/adimage/2.jpg | http://www.nepalfashion.com | 2011-10-
18 15:53:06 | 2011-11-03 10:59:57 | 2011-11-18 15:53:06 | 15 | 1 | 14 |
+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------
我該如何解決這個問題?
在此先感謝。 詹姆斯
我應該在發佈之前嘗試過自己。它使用了一個簡單的調整。在ON後更改第一個AND之後工作到WHERE。 – James