[英]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.