mysql - 如何合并mysql中四个没有共同点的表

标签 mysql

我有四个表,它们之间没有任何共同点。我的四个表的列很少:

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/

相关文章:

php - 在 codeigniter 中向 Controller 注入(inject) SQL

php - 查询在 XAMPP 中有效,但在 AWS 云中的 LAMP 堆栈中无效

MYSQL的创建和连接

mysql - 三个表的查询结果

mysql - 联合语句仅给出引用一列的错误

php - 循环插入查询有效一次,但之后无效

mysql - 无法在 MySQL 上创建 View

mysql - 连接类别和子类别

php - 定义方法 CI_DB_mysqli_result::where()

c# - 使用本地服务的成员资格表不正确