2017-10-05 19 views
0

我試圖獲得具有平均參與率的表的日期列表。但目前它正在略過列表中的日期,如果它們不存在於媒體表中。因此,如果該行存在的犯規,我想參與度爲0如果行不存在,則mysql左連接,使用0

例如(目前)

2017-09-30 - 123 
2017-09-28 - 1234 
2017-09-27 - 12345 

,但它應該是

2017-09-30 - 123 
2017-09-29 - 0 
2017-09-28 - 1234 
2017-09-27 - 12345 

這裏是我的查詢

"select d.date AS created_at, 
       AVG((IFNULL(v.likes, 0) + IFNULL(v.comments, 0))/
       IFNULL((SELECT count FROM followers prev WHERE DATE(prev.created_at) = DATE(v.created_at) 
                AND prev.profile_id = '".$this->profile->id."' 
                ORDER BY created_at 
                DESC LIMIT 1), 0) * 100) 
                AS count 
       from 
       (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) date from 
       (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, 
       (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, 
       (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, 
       (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3, 
       (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) d 
       left join media v on d.date = DATE(v.created_at) 
       where d.date between '".Carbon::createFromFormat('Y-m-d', $this->startDate)->toDateTimeString()."' and '".Carbon::createFromFormat('Y-m-d', $this->endDate)->toDateTimeString()."' 
       AND v.profile_id = '".$this->profile->id."' 
       group by d.date 
       order by d.date DESC" 
+0

這不是一個查詢,這是一個字符串。 – HoneyBadger

+0

以及即時通訊使用laravel,但我並不想要全部粘貼 – Dan

+1

認真考慮處理應用程序代碼 – Strawberry

回答

0

要獲得真正的LEFT JOIN結果,將表V的條件從WHEREON條款:

 left join media v on d.date = DATE(v.created_at) AND v.profile_id = '".$this->profile->id."' 
     where d.date between '".Carbon::createFromFormat('Y-m-d', $this->startDate)->toDateTimeString()."' and '".Carbon::createFromFormat('Y-m-d', $this->endDate)->toDateTimeString()."' 
+0

非常感謝! 我以爲你只能在WHERE子句中使用AND – Dan