我正在嘗試編寫一個SQL查詢,它將基於日期(數據庫中的INTEGER)進行排序,該日期可能位於兩個表中的一個或兩個表中。mysql中的條件選擇變量
表,這是在問候中的字段有:
分配: - ID - id_lead - date_assigned
線索: - ID - DATE_ADDED
的表已其他領域,但他們不適用於這個問題。
現在我想要的是,如果沒有分配給主角,我希望「date_sort」變量是主角的date_added值。如果有主角的任務,我希望它是任務的date_assigned值。
我的問題是,使用我的查詢只是不顯示任何在我的結果列表中沒有任務的線索。
繼承人我查詢即時工作:
$pagi_sql = "select
l.id, l.id_infusionsoft, l.name_first, l.name_last, l.postcode, a.website as a_website, l.website as l_website, l.date_added,
a.id_dealership, a.id_lead, a.id as id_assign, a.date_assigned,
d.name as dealership,
if(a.id = null, l.date_added, a.date_assigned) as date_sort
from `leads` as l
left join `assignments` as a on (a.id_lead = l.id)
left join `dealerships` as d on (d.id = a.id_dealership)
$where
order by date_sort desc";
繼承人我原來的查詢,其工作,但不排序我希望它的方式:
$pagi_sql = "select
l.id, l.id_infusionsoft, l.name_first, l.name_last, l.postcode, a.website as a_website, l.website as l_website, l.date_added,
a.id_dealership, a.id_lead, a.id as id_assign, a.date_assigned,
d.name as dealership
from `leads` as l
left join `assignments` as a on (a.id_lead = l.id)
left join `dealerships` as d on (d.id = a.id_dealership)
$where
order by l.date_added desc";
引線可以有多個任務。 ...我認爲這是問題...但我無法弄清楚如何讓它按我想要的方式排序。
我應該改變這些,以便即時從分配表中選擇,而不是?但後來我遇到了問題,我如何顯示按照date_added/date_assigned排序的同一個表中未分配的線索?
任何幫助將是可怕的。
'ORDER BY CASE WHEN a.ID爲null,則l.date_added ELSE一個。 date_assigned END desc' – 2012-07-17 18:55:32