输出两个表的表名和其他属性

时间:2016-11-20 18:07:36

标签: mysql sql

如果我有两张这样的表:

-- -----------------------------------------------------
-- Table `mydb77`.`supplier`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb77`.`supplier` (
  `sp_id` INT(11) NOT NULL AUTO_INCREMENT,
  `sp_company` VARCHAR(45) NULL DEFAULT NULL,
  `sp_location` VARCHAR(45) NULL DEFAULT NULL,
  PRIMARY KEY (`sp_id`));

-- -----------------------------------------------------
-- Table `mydb77`.`customer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb77`.`customer` (
  `cu_id` INT(11) NOT NULL AUTO_INCREMENT,
  `cu_name` VARCHAR(45) NULL DEFAULT NULL,
  `cu_title` VARCHAR(15) NULL DEFAULT NULL,
  `cu_location` VARCHAR(45) NULL DEFAULT NULL,
  PRIMARY KEY (`cu_id`);

如何获得以下输出:

enter image description here

按名称订购。

表格的列数不同。

1 个答案:

答案 0 :(得分:2)

您可以使用union all执行所需操作:

select 'supplier' as tablename, sp_id as id,
       sp_company as name, sp_location as location
from supplier
union all
select 'customer', cu_id, cu_company, cu_location
from customer
order by 3;