加入过滤器在MySQL中没有按预期工作

时间:2011-11-04 09:53:00

标签: mysql algorithm join left-join

我编写了一个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,但在结果中,我也得到了Status!= 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 |
+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------

我该如何解决这个问题?

提前致谢。 詹姆斯

2 个答案:

答案 0 :(得分:1)

你应该将a.Status = 1移动到where子句:

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
    WHERE
        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;

答案 1 :(得分:0)

我假设这个问题实际上应该是

  

我正在获取Status!= 1的行

原因是您a.Status = 1作为LEFT JOIN的加入条件的一部分。因此,正确的解决方案将使用INNER JOIN或将该过滤器作为where子句的一部分。