一共有三张表,其中两张表没有关联,希望能将三张表合并查询

我有三张表
bulldozer_info 出工地表,有1万条数据
refuse_treatment_plant 进消纳厂表,有100条数据
input_output_status 驶入驶出状态表

bulldozer_info和refuse_treatment_plant之间没有字段关联

在bulldozer_info中有字段id、license_number、begin_time、end_time、driving_status

在refuse_treatment_plant表有字段id、license_number、plant_name、entry_time、driving_status

input_output_status 驶入驶出状态表
这表有字段id, status_id, status_explanation

我希望将bulldozer_info和refuse_treatment_plant的数据都查出来,一共是1万100条,

然后字段包括bulldozer_info.id、refuse_treatment_plant.id、license_number、begin_time、end_time、entry_time、driving_status、plant_name、entry_time、driving_status、status_explanation

如果没有则为null

然后按照begin_time和entry_time进行排序

阅读 2.3k
1 个回答

使用子查询+UNION ALL, 两个表的字段数量要一致, 没有的字段就是 常量填充.

select * from   (
    select 表1的字段, ... from 表1
    UNION ALL
    select 表2的字段, ... from 表2
) as t
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题