Customers 顾客信息表有字段 cust_id(顾客id)、cust_name(顾客名称) cust_id cust_name cust10 andy cust1 ben cust2 tony cust22 tom cust221 an cust2217 hex Orders 订单信息表有字段 order_num(订单号)、cust_id(顾客id) order_num cust_id a1 cust10 a2 cust1 a3 cust2 a4 cust22 a5 cust221 a7 cust2217 【问题】 编写 SQL 语句,返回 Customers 表中的顾客名称(cust_name)和 Orders 表中的相关订单号(order_num),只显示有订单的客户,并先按顾客名称再按订单号对结果进行升序排序。 【示例结果】 cust_name order_num an a5 andy a1 ben a2 hex a7 tom a4 tony a3 【示例解析】顾客名称为 an 的 cust_id 为 cust221,他的订单号为 a5。
示例1

输入

DROP TABLE IF EXISTS `Orders`;
CREATE TABLE IF NOT EXISTS `Orders`(
  order_num VARCHAR(255) NOT NULL COMMENT '商品订单号',
  cust_id VARCHAR(255) NOT NULL COMMENT '顾客id'
);
INSERT `Orders` VALUES ('a1','cust10'),('a2','cust1'),('a3','cust2'),('a4','cust22'),('a5','cust221'),('a7','cust2217');

DROP TABLE IF EXISTS `Customers`;
CREATE TABLE IF NOT EXISTS `Customers`(
	cust_id VARCHAR(255) NOT NULL COMMENT '客户id',
	cust_name VARCHAR(255) NOT NULL COMMENT '客户姓名'
);
INSERT `Customers` VALUES ('cust10','andy'),('cust1','ben'),('cust2','tony'),('cust22','tom'),('cust221','an'),('cust2217','hex');

输出

an|a5
andy|a1
ben|a2
hex|a7
tom|a4
tony|a3
加载中...