开发者

Display two different columns from two different tables with ORDER BY

开发者 https://www.devze.com 2023-03-23 02:50 出处:网络
I want to display the values of two different tables, and two different columns from that table, sorted in ascending order.

I want to display the values of two different tables, and two different columns from that table, sorted in ascending order.

How can I do that?

I use this to display values of two tables, but stuck in sorting by ascending order:

select 
  distinct varWinePrice 
from 
  tbl_wines 
union开发者_C百科 
select 
  distinct varPrice 
from 
  tbl_price


If column names are different then sorting should be done using the column of the first subselect:

select distinct varWinePrice 
from `tbl_wines` 
union 
select distinct varPrice 
from tbl_price
order by varWinePrice


select distinct varWinePrice as x from tbl_wines union
select distinct varPrice as x from tbl_price
order by x asc;


you may nest your UNION select into outer select

select
  pr.price
from (
  select 
    distinct varWinePrice 
  from 
    tbl_wines 
  union 
  select 
    distinct varPrice 
  from 
    tbl_price
) pr
order by
  pr.price


You can use temporary table to use order by syntax:
1) Create temporary table
2) Fill the temporary table with records
3) get the rows from temporary table by applying order by clause


SELECT Price
FROM ( SELECT DISTINCT varWinePrice AS Price FROM tbl_wines 
       UNION
       SELECT distinct varPrice AS Price FROM tbl_price
     )
ORDER BY Price
0

精彩评论

暂无评论...
验证码 换一张
取 消