我正在尝试通过使用以下查询来获取数据,但这里的问题是它不显示advert_customer其中不可用的数据advert_abstract。在这种情况下IF,LEFT 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然后我也会得出其余的详细信息。
任何建议都会有所帮助,谢谢(提前)!