以下是两张表:
Create table If Not Exists Customers (Id int, Name varchar(255));
Create table If Not Exists Orders (Id int, CustomerId int);
insert into Customers (Id, Name) values ('1', 'Joe');
insert into Customers (Id, Name) values ('2', 'Henry');
insert into Customers (Id, Name) values ('3', 'Sam');
insert into Customers (Id, Name) values ('4', 'Max');
insert into Orders (Id, CustomerId) values ('1', '3');
insert into Orders (Id, CustomerId) values ('2', '1');
insert into Orders (Id, CustomerId) values ('3', '5');
insert into Orders (Id, CustomerId) values ('4', '6');
我将两个表的左连接和右连接合并:
select *
from customers as c
left join orders as o
on c.id = o.customerid
union
select *
from customers as c
right join orders as o
on c.id = o.customerid;
但是我想从表customers订购id设置的结果:
select
*
from
customers as c
left join
orders as o
on
c.id = o.customerid
union
select
*
from
customers as c
right join
orders as o
on
c.id = o.customerid
order by
c.id;
但是mysql的响应是“错误代码:1250。其中一个SELECT中的表'c'不能用于“全局顺序子句”。所以我把代码改成:
select
*
from
customers as c
left join
orders as o
on
c.id = o.customerid
union
select
*
from
customers as c
right join
orders as o
on
c.id = o.customerid
order by
id;
但是mysql的响应是“错误代码:1052。order子句中的“id”列不明确。
我真的理解这两个答复,但我也想知道如何通过id订购后,从客户联盟?
2条答案
按热度按时间kgsdhlau1#
是的,您需要通过将当前查询作为子查询来进行全局排序,就像通过分别别名id列:
7rtdyuoh2#
也许你需要:
小提琴