0

我正在尝试通过使用以下查询来获取数据,但这里的问题是它不显示advert_customer其中不可用的数据advert_abstract。在这种情况下IFLEFT JOIN我认为最重要的问题是。

以下是表结构:

advert_customer

+-----+-------------+--------------------+--------+--------------+--------+---------------------+---------------------+-----+--------+---------------------+
| id  | customer_id | title              | budget | registertype | active | start_date          | end_date            | web | gender | created_at          |
+-----+-------------+--------------------+--------+--------------+--------+---------------------+---------------------+-----+--------+---------------------+
| 341 |           3 | test               |  22.00 | paid         |      1 | 2018-11-15 00:00:00 | 2018-11-22 00:00:00 |   1 | all    | 2018-11-15 12:01:07 |
| 340 |           3 | demo testing       |  15.00 | paid         |      1 | 2018-11-15 00:00:00 | 2018-11-25 00:00:00 |   1 | all    | 2018-11-15 10:52:36 |
| 339 |           3 | test data          |   8.00 | paid         |      0 | 2018-11-01 00:00:00 | 2018-11-08 00:00:00 |   1 | all    | 2018-11-14 10:38:57 |
| 336 |           3 | fgdfg              |  18.00 | paid         |      0 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |   1 | all    | 2018-11-13 16:56:52 |
| 335 |           3 | Demo Testing       |   7.00 | paid         |      1 | 2018-11-12 00:00:00 | 2018-11-14 00:00:00 |   0 | all    | 2018-11-12 15:22:49 |
| 334 |           3 | Demo Advertisement |  16.00 | paid         |      1 | 2018-11-12 00:00:00 | 2018-11-17 00:00:00 |   0 | female | 2018-11-12 15:02:36 |
| 333 |           3 | today ads          |  11.00 | paid         |      1 | 2018-11-11 00:00:00 | 2018-11-15 00:00:00 |   1 | male   | 2018-11-05 18:13:45 |
| 331 |         237 | testin15           |  37.00 | paid         |      0 | 2018-10-28 00:00:00 | 2018-10-31 00:00:00 |   0 | male   | 2018-11-04 17:14:32 |
+-----+-------------+--------------------+--------+--------------+--------+---------------------+---------------------+-----+--------+---------------------+

另一张桌子advert_abstract

+----+--------------------+-------------+---------+------------------------+-------------+--------------+---------------------------+---------------------+---------------------+
| id | advert_customer_id | customer_id | post_id | impression_count_login | click_count | amount_spent | impression_count_nonlogin | created_at          | updated_at          |
+----+--------------------+-------------+---------+------------------------+-------------+--------------+---------------------------+---------------------+---------------------+
| 12 |                 41 |           3 |    NULL |                   1020 |        NULL |         0.00 |                         1 | 2018-11-15 09:52:58 | 2018-11-15 12:20:04 |
| 11 |                 32 |           3 |    NULL |                   1013 |        NULL |         0.00 |                         1 | 2018-11-15 09:50:01 | 2018-11-15 12:20:04 |
| 10 |                 12 |           3 |    NULL |                     20 |        NULL |         0.00 |                        15 | 2018-11-14 12:49:47 | 2018-11-14 12:49:47 |
|  9 |                111 |           3 |    NULL |                     20 |        NULL |         0.00 |                      NULL | 2018-11-14 12:38:53 | 2018-11-14 12:38:53 |
|  8 |                111 |           3 |    NULL |                     10 |        NULL |         0.00 |                      NULL | 2018-11-14 12:38:36 | 2018-11-14 12:38:36 |
|  6 |                 41 |           3 |    NULL |                   1004 |        NULL |         0.00 |                         1 | 2018-11-14 10:05:40 | 2018-11-14 18:33:01 |
|  5 |                 32 |           3 |    NULL |                   1005 |        NULL |         0.00 |                         1 | 2018-11-14 10:05:26 | 2018-11-14 18:33:01 |
|  4 |                333 |           3 |    NULL |                     15 |        NULL |         0.00 |                         1 | 2018-11-14 10:05:26 | 2018-11-14 10:44:01 |
+----+--------------------+-------------+---------+------------------------+-------------+--------------+---------------------------+---------------------+---------------------+

如果此表的 JOIN 中有任何行,则问题在于放置左连接,advert_abstract那么它应该返回数据,否则这些列将为 NULL。

select `eac`.`id`, `eac`.`gender`, `eac`.`start_date`, `eac`.`end_date`, `eac`.`ad_image_path`, `eac`.`ad_link`, `eac`.`requestfrom`, `eac`.`traffic`, `eac`.`position`, `eac`.`registertype`, `eacr`.`region_id`, `eac`.`active`, `eac`.`impression`, `eac`.`center_image_path`, `eac`.`bottom_image_path`, `eac`.`approved_by`, `er`.`name` as `country_name`, `eac`.`budget`, sum(budget/ (DATEDIFF(end_date,start_date)) * 1000) as daily_imp,`eaa`.`impression_count_login`,`eaa`.`impression_count_nonlogin`,sum(impression_count_login + impression_count_nonlogin) as impression_count , `eac`.`customer_id`, `eaa`.`created_at`,`eac`.`web` 

from `advert_customer` as `eac` 

inner join `advert_customer_regions` as `eacr` on `eac`.`id` = `eacr`.`advert_customer_id` 

inner join `regions` as `er` on `er`.`id` = `eacr`.`region_id`                                  

left join `advert_abstract` as `eaa` on `eac`.`id` =`eaa`.`advert_customer_id`             <<<<<<<-----------------------    I WANT THIS TO HAPPEN IF THERE IS ANY ROW IN THE TABLE FROM CURENT DATE

  where `er`.`name` = 'India' and 
        `eac`.`web` = 1 and
        `eac`.`registertype` = 'paid' and
        `eac`.`active` = 1 and 
        `eac`.`approved_by` = 1 and 
        `eac`.`gender` = 'all' and 
        DATE(eac.start_date) <= DATE(CURDATE()) and
        DATE(eac.end_date) >=   DATE(CURDATE()) and

IF((SELECT CASE WHEN DATE(eaa.created_at) = DATE(CURDATE()) THEN eaa.created_at ELSE NULL END), eaa.created_at , NULL)                  <<<<<<<-----------------------    ON THIS LINE I THINK THE ISSUE IS

group by `eac`.`id`, `eac`.`gender`, `eac`.`start_date`, `eac`.`end_date`, `eac`.`ad_image_path`, `eac`.`ad_link`, `eac`.`requestfrom`, `eac`.`traffic`, `eac`.`position`, `eac`.`registertype`, `eacr`.`region_id`, `eac`.`active`, `eac`.`impression`, `eac`.`center_image_path`, `eac`.`bottom_image_path`, `eac`.`approved_by`, `eac`.`budget`, `eac`.`customer_id`, `eac`.`web`
                                       HAVING impression_count < daily_imp
                                        ORDER BY RAND() LIMIT 25

我不确定是否可以编写IFANDLEFT JOIN条件,或者是否有任何其他 JOIN 方法我应该尝试从中获取数据。因此,如果数据不可用,我可以获取所有数据,advert_abstract然后我也会得出其余的详细信息。

任何建议都会有所帮助,谢谢(提前)!

4

2 回答 2

0

我认为你只需要包括一个“加入条件”。换句话说,将想要的过滤器直接放入连接中,并且不要包含在 where 子句中。这样,左连接将仅包含该表中符合该条件的行

选择
    eac.id、eac.gender、eac.start_date、eac.end_date、eac.ad_image_path
  , eac.ad_link, eac.requestfrom, eac.traffic, eac.position, eac.registertype
  , eacr.region_id, eac.active, eac.impression, eac.center_image_path
  , eac.bottom_image_path, eac.approved_by, er.name AS country_name, eac.budget
  , eaa.impression_count_login, eaa.impression_count_nonlogin
  , eac.customer_id, eaa.created_at, eac.web
  , SUM (预算 / (DATEDIFF (end_date, START_DATE)) * 1000) AS daily_imp
  , SUM (impression_count_login + impression_count_nonlogin) AS 印象数
FROM advert_customer AS eac
INNER JOIN advert_customer_regions AS eacr ON eac.id = eacr.advert_customer_id
INNER JOIN 区域 AS er ON er.id = eacr.region_id

  # 如果当前日期的表格中有任何行,我希望发生这种情况
LEFT JOIN advert_abstract AS eaa ON eac.id = eaa.advert_customer_id
                                和日期(eaa.created_at)= CURDATE()

WHERE er.name = '印度'
和 eac.web = 1
和 eac.registertype = '付费'
AND eac.active = 1
AND eac.approved_by = 1
AND eac.gender = '全部'
AND CURDATE() 在 eac.start_date 和 eac.end_date 之间
通过...分组
    eac.id、eac.gender、eac.start_date、eac.end_date、eac.ad_image_path
  , eac.ad_link, eac.requestfrom, eac.traffic, eac.position, eac.registertype
  , eacr.region_id, eac.active, eac.impression, eac.center_image_path
  , eac.bottom_image_path, eac.approved_by, er.name, eac.budget
  , eaa.impression_count_login, eaa.impression_count_nonlogin
  , eac.customer_id, eaa.created_at, eac.web

请注意您不需要使用date(curdate()),因为curdate()它只是一个日期。我还建议在上面的查询中使用 between,我认为它更容易理解。

这些行中缺少一些列别名,请参阅???.

, SUM (???.budget / (DATEDIFF (???.end_date, ???.START_DATE)) * 1000) AS daily_imp
, SUM (???.impression_count_login + ???.impression_count_nonlogin) AS impression_count

使用适当的别名引用所有列非常重要。

于 2018-11-15T10:50:12.857 回答
0

你并没有真正说出你想要达到的目标。所以我只能猜测......但这里有一个建议:

select `eac`.`id`, `eac`.`gender`, `eac`.`start_date`, `eac`.`end_date`, 
`eac`.`ad_image_path`, `eac`.`ad_link`, `eac`.`requestfrom`, 
`eac`.`traffic`, `eac`.`position`, `eac`.`registertype`, `eacr`.`region_id`, 
`eac`.`active`, `eac`.`impression`, `eac`.`center_image_path`, 
`eac`.`bottom_image_path`, `eac`.`approved_by`, `er`.`name` as `country_name`, 
`eac`.`budget`, 
sum(budget/ (DATEDIFF(end_date,start_date)) * 1000) as daily_imp,  
`eaa`.`impression_count_login`,`eaa`.`impression_count_nonlogin`,
sum(impression_count_login + impression_count_nonlogin) as impression_count , 
`eac`.`customer_id`, 
CASE WHEN `eaa`.`created_at` = DATE(CURDATE())  THEN DATE(CURDATE())
     WHEN `eaa`.`created_at` IS NULL            THEN NULL
     ELSE NULL
END AS created_at,
`eac`.`web` 

from `advert_customer` as `eac` 

inner join `advert_customer_regions` as `eacr` on `eac`.`id` = 
`eacr`.`advert_customer_id` 

inner join `regions` as `er` on `er`.`id` = `eacr`.`region_id`                                  

left join `advert_abstract` as `eaa` on `eac`.`id` =`eaa`.`advert_customer_id`         

 where `er`.`name` = 'India' and 
    `eac`.`web` = 1 and
    `eac`.`registertype` = 'paid' and
    `eac`.`active` = 1 and 
    `eac`.`approved_by` = 1 and 
    `eac`.`gender` = 'all' and 
    DATE(eac.start_date) <= DATE(CURDATE()) and
    DATE(eac.end_date)   >=   DATE(CURDATE())            

group by `eac`.`id`, `eac`.`gender`, `eac`.`start_date`, `eac`.`end_date`, 
`eac`.`ad_image_path`, `eac`.`ad_link`, `eac`.`requestfrom`, `eac`.`traffic`, 
`eac`.`position`, `eac`.`registertype`, `eacr`.`region_id`, `eac`.`active`, 
`eac`.`impression`, `eac`.`center_image_path`, `eac`.`bottom_image_path`, 
`eac`.`approved_by`, `eac`.`budget`, `eac`.`customer_id`, `eac`.`web`
                                   HAVING impression_count < daily_imp
                                    ORDER BY RAND() LIMIT 25

那么它有什么作用呢?它实现了与表的正确左连接,advert_abstract因此advert_customer即使advert_abstract. 其次,它只显示“created_at”日期,如果它存在并且它等于当前日期。否则,该字段返回 null。

于 2018-11-15T10:42:38.853 回答