简体   繁体   English

获取记录但不通过与工会合作来订购

[英]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. 我有两个表,并且正在使用UNION从两个表中获取记录。 I am getting the records but records are not displaying in DESC order. 我正在获取记录,但记录未按DESC顺序显示。 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 : 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

Sorting each subquery and then applying UNION ALL does not mean the result will be sorted. 对每个子查询进行排序,然后应用UNION ALL并不意味着将对结果进行排序。
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 : 或者,如果您不想选择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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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