简体   繁体   English

联接过滤器在MySQL中无法按预期工作

[英]Join filter not working as expected in MySQL

I have written a MySQL query (Ad rotate algorithm) to fetch records from multiple tables. 我编写了一个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;

The above query has a filter a.Status = 1 , but in the result, i'm getting rows with Status != 1 as well. 上面的查询有一个过滤器a.Status = 1 ,但是在结果中,我也得到了Status!= 1的行。 Resultset below: 结果集如下:

+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------
------------+---------------------+---------------------+--------+--------+-----------+
| 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 |
+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------

How can I fix this? 我怎样才能解决这个问题?

Thanks in advance. 提前致谢。 James 詹姆士

You should move and a.Status = 1 to a where clause: 您应该将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;

I'm assuming the question should actually be 我假设问题实际上应该是

i'm getting rows with Status != 1 as well 我也收到状态为!= 1的行

The reason is because you have a.Status = 1 as part of the join condition of a LEFT JOIN . 原因是因为LEFT JOIN条件中有a.Status = 1 So the correct solution will either use an INNER JOIN or put that filter as part of a where clause. 因此,正确的解决方案将使用INNER JOIN或将该过滤器作为where子句的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM