mysql - How to union four tables in mysql where there is nothing common among them
问题描述
I have four tables where there is nothing common among them.My four tables have few columns they are:
Table 1: EpId,Name,Position,Email,ManagerId,To/From
Table 2: Type
Table 3: LocId,LocName
Table 4: PreName
The expected output is
EpId Name Position email managerId To/From Type LocId LocName PreName
The query so far is :
(select u.EpId as id,u.Name as Name,
u.Position as Position,u.ManagerId as ManagerId,u.Email as Email
from Employee as u)
union
(select c.Type as Type from EpType as c)
union
(select l.LocId as LocId,l.LocName as LocName from Location)
union
(select p.Name as PreName from Premise)
But it throws error : The used SELECT statements have a different number of columns
解决方案
use cross join
, but seems you just want to get 1 for each other tables, you use limit
select distinct t1.EpId, t1.Position, t1.email, t1.managerId, t2.Type, t3.LocId , t3.LocName , t4.PreName from
Employee t1
cross join (select Type as Type from EpType limit 1)t2
cross join (select LocId as LocId,LocName as LocName from Location limit 1) t3
cross join (select Name as PreName from Premise limit 1) t4
推荐阅读
- android - 如何从 Kotlin 中的 RequestBody 获取正文字符串?
- typescript - 确保两个元组参数具有相同数量的元素
- html - 在我的 html 列表中应用图标和文本时遇到缩进问题。如何正确缩进项目?
- flutter - 小部件截断 - Flutter
- javascript - redux 形式的字段规范化
- windows - 如何在 Windows 文件资源管理器中将我的自定义图像文件类型预览为缩略图?
- java - 如何在我的 Android 应用程序中将 Instagram 分享添加到故事按钮
- vue.js - 如何在 VueJS 中有一个作用域切换变量
- python-3.x - 将 CSV 导入 Django,保存数据并在模板中显示结果
- linux - 如何在 bash shell Linux 中使用两个单词查找路径