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,846
  • 2
  • 23
  • 48

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
  • 170,088
  • 45
  • 397
  • 571
step
  • 2,254
  • 2
  • 23
  • 45
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
  • 15,408
  • 6
  • 37
  • 48