0

I have a problem joining tables in the result column. i have a working query which combine different tables using UNION but when i'm extending another table i got an error saying 'The used SELECT statements have a different number of columns'

this is my query:

(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')

I have problem with this. Continuation query above

UNION
(SELECT
    Barrier
FROM 
    tblinformation as inf
WHERE
    inf.RFIDNo IN (6424823943)
ORDER BY 
    AttendanceNo DESC LIMIT 1)
Shidersz
  • 16,261
  • 2
  • 18
  • 44

2 Answers2

0

The error message states what the problem is. Just improve number of columns in SELECT and it will work correctly.

Brad Larson
  • 169,393
  • 45
  • 393
  • 567
step
  • 1,905
  • 2
  • 22
  • 41
0

The error message is correct. Add NULLs to your second query to match the column number, and it will work.

For example:

SELECT
    Barrier,
    NULL,
    NULL,
    NULL,
    NULL
FROM 
    tblinformation as inf
...
gaborsch
  • 14,964
  • 5
  • 35
  • 46