如何在另一个表中合并表

tp5buhyn  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(315)

在结果列中联接表时遇到问题。我有一个工作查询,它使用union组合不同的表,但是当我扩展另一个表时,我得到一个错误,说“使用的select语句有不同的列数”
我的问题是:

(SELECT
    IDNumber,
    CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
    CONCAT(EmDesignation, ', ', Department) as Information,
    Image,
    v.PlateNo as PlateNumber
FROM
    tblemployee as e, tblvehicle as v
WHERE 
    v.RFIDNo LIKE '6424823943'
AND
    e.RFIDNo LIKE '6424823943')
UNION
(SELECT 
    IDNumber,
    CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
    CONCAT(Course, ', ', Year) as Information,
    Image,
    v.PlateNo as PlateNumber
FROM
    tblstudents as s, tblvehicle as v
WHERE
    v.RFIDNo LIKE '6424823943'
AND
    s.RFIDNo LIKE '6424823943')

我对此有意见。上面的继续查询

UNION
(SELECT
    Barrier
FROM 
    tblinformation as inf
WHERE
    inf.RFIDNo IN (6424823943)
ORDER BY 
    AttendanceNo DESC LIMIT 1)
rn0zuynd

rn0zuynd1#

错误消息说明问题所在。只要增加select中的列数,它就会正常工作。

myzjeezk

myzjeezk2#

错误消息是正确的。添加 NULL s到第二个查询以匹配列号,它将起作用。
例如:

SELECT
    Barrier,
    NULL,
    NULL,
    NULL,
    NULL
FROM 
    tblinformation as inf
...

相关问题