[英]Getting records but order by not working with union
我有兩個表,並且正在使用UNION從兩個表中獲取記錄。 我正在獲取記錄,但記錄未按DESC順序顯示。 我的意思是按命令不起作用。
(
select l.c_id,l.companyname, l.c_firstname, l.c_lastname, emp.firstname as empfirstname,emp.lastname as emplastname
from tbl_lead as l
inner join tbl_employee as emp on l.createby=emp.id
WHERE l.leadstatus IN (2,3)
ORDER BY l.date_of_created DESC
) UNION ALL (
select l.c_id,l.companyname, l.c_firstname, l.c_lastname, emp.firstname as empfirstname,emp.lastname as emplastname
from tbl_leadUpload as l
inner join tbl_employee as emp on l.createby=emp.id
where l.leadstatus IN (2,3)
ORDER BY l.date_of_created DESC
)
您能幫我解決這個問題嗎?
將ORDER BY
子句帶到兩個子查詢共同使用的主子查詢:
select c_id, companyname, c_firstname, c_lastname, empfirstname, emplastname
from
(
select l.date_of_created,l.c_id,l.companyname, l.c_firstname, l.c_lastname,
emp.firstname as empfirstname,emp.lastname as emplastname
from tbl_lead as l inner join tbl_employee as emp on l.createby=emp.id
where l.leadstatus in (2,3)
union all
select l.date_of_created,l.c_id,l.companyname, l.c_firstname, l.c_lastname,
emp.firstname as empfirstname,emp.lastname as emplastname
from tbl_leadUpload as l inner join tbl_employee as emp on l.createby=emp.id
where l.leadstatus in (2,3)
) q
order by date_of_created desc
對每個子查詢進行排序,然后應用UNION ALL
並不意味着將對結果進行排序。
您可以做的是也選擇您要排序的列,並對結果進行排序:
select
l.date_of_created, l.c_id, l.companyname, l.c_firstname, l.c_lastname,
emp.firstname as empfirstname, emp.lastname as emplastname
from tbl_lead as l inner join tbl_employee as emp
on l.createby=emp.id
WHERE l.leadstatus IN (2,3)
UNION ALL
select
l.date_of_created, l.c_id, l.companyname, l.c_firstname, l.c_lastname,
emp.firstname as empfirstname, emp.lastname as emplastname
from tbl_leadUpload as l inner join tbl_employee as emp
on l.createby=emp.id
where l.leadstatus IN (2,3)
ORDER BY date_of_created DESC
或者,如果您不想選擇date_of_created
:
select
t.c_id, companyname, t.c_firstname, t.c_lastname, t.empfirstname, t.emplastname
from (
select
l.date_of_created, l.c_id, l.companyname, l.c_firstname, l.c_lastname,
emp.firstname as empfirstname, emp.lastname as emplastname
from tbl_lead as l inner join tbl_employee as emp
on l.createby=emp.id
WHERE l.leadstatus IN (2,3)
UNION ALL
select
l.date_of_created, l.c_id, l.companyname, l.c_firstname, l.c_lastname,
emp.firstname as empfirstname, emp.lastname as emplastname
from tbl_leadUpload as l inner join tbl_employee as emp
on l.createby=emp.id
where l.leadstatus IN (2,3)
) as t
ORDER BY t.date_of_created DESC
select l.c_id, l.companyname, l.c_firstname, l.c_lastname,
e.firstname as empfirstname, e.lastname as emplastname
from ((select l.created_by, l.date_of_created, l.c_id, l.companyname, l.c_firstname, l.c_lastname
from tbl_lead l
where l.leadstatus in (2, 3)
) union all
(select l.created_by, l.date_of_created, l.c_id, l.companyname, l.c_firstname, l.c_lastname
from tbl_leadupdate l
where l.leadstatus in (2, 3)
)
) l join
tbl_employee as emp
on l.createby = emp.id
order by l.date_of_created desc;
如果兩個“線索”表具有相同的列,則需要更少的輸入:
select l.c_id, l.companyname, l.c_firstname, l.c_lastname,
e.firstname as empfirstname, e.lastname as emplastname
from ((select l.*
from tbl_lead l
where l.leadstatus in (2, 3)
) union all
(select l.*
from tbl_leadupdate l
where l.leadstatus in (2, 3)
)
) l join
tbl_employee as emp
on l.createby = emp.id
order by l.date_of_created desc;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.