简体   繁体   中英

Getting records but order by not working with union

I have two tables and I am using UNION to get the records from both tables. I am getting the records but records are not displaying in DESC order. I mean order by is not working.

(
  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
)

would you help me out with this issue?

Take ORDER BY Clause to the main subquery to be used commonly for the both of the subqueries :

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

Sorting each subquery and then applying UNION ALL does not mean the result will be sorted.
What you can do is select also the column that you want to order by and apply sorting at the result:

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

or if you don't want to select 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;

If the two "leads" tables have the same columns, even less typing is needed:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM