Фильтр соединения не работает должным образом в MySQL - PullRequest
0 голосов
/ 04 ноября 2011

Я написал запрос 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 ]

1 голос
/ 04 ноября 2011

Вы должны переместиться и 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;
0 голосов
/ 04 ноября 2011

Я предполагаю, что вопрос на самом деле должен быть

Я также получаю строки с Status! = 1

Причина в том, что у вас есть a.Status = 1 как часть условия соединения LEFT JOIN. Таким образом, правильное решение будет использовать INNER JOIN или поместить этот фильтр как часть предложения where.

...