我有四个表,它们之间没有任何共同点。我的四个表的列很少:
Table 1: EpId,Name,Position,Email,ManagerId,To/From
Table 2: Type
Table 3: LocId,LocName
Table 4: PreName
预期输出是
EpId Name Position email managerId To/From Type LocId LocName PreName
到目前为止的查询是:
(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)
但它抛出错误: 使用的 SELECT 语句具有不同的列数
最佳答案
使用交叉联接
,但似乎您只想为每个其他表获取1,您使用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
关于mysql - 如何合并mysql中四个没有共同点的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58832636/