Using Union All and Order By in MySQL

前端 未结 2 1396
春和景丽
春和景丽 2021-01-19 03:50

I\'ve 2 tables:

create table advertised_products(id int,title varchar(99),timestamp timestamp);
insert advertised_products select 1,\'t1\',curdate();

create         


        
2条回答
  •  不知归路
    2021-01-19 04:25

    Wrap it in a subquery.

    SELECT s.*
    FROM
        (
            SELECT  ap.*, 'advertised'  as type 
            FROM advertised_products as ap
              union all
            SELECT  wp.*, 'wanted' as type 
            FROM wanted_products as wp
        ) s
    ORDER BY s.timestamp desc 
    limit 3
    

提交回复
热议问题