两张表结构

CREATE TABLE `t1`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `t1_name` char(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  `create_time` int(11) UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `t2`  (
  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `t2_name` char(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  `create_time` int(11) UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

利用as将查询字段设置为一致 然后UNION ALL以后再order by 多张表依次类推

select id,t1_name as `name`,create_time,'t1' as table_name from t1
UNION ALL
select id,t2_name as `name`,create_time,'t2' as table_name from t2
ORDER BY create_time DESC
最后修改:2020 年 11 月 05 日 05 : 05 PM
如果觉得我的文章对你有用,请随意赞赏