简体   繁体   中英

query SELECT statements have a different number of columns

I am getting this with the following code:

The used SELECT statements have a different number of columns. I still searched the solution from this site but i can not found the what is the problem in my query. Anyone can help me here why i am getting = "The used SELECT statements have a different number of columns" and How can i fix it ?

SELECT DISTINCT 
            M.msg_id, 
            M.uid_fk, 
            M.message,
            S.created, 
            M.share_count, 
            U.username,
            U.last_login,
            M.uploads, 
            S.uid_fk AS 
            share_uid,
            S.ouid_fk AS share_ouid FROM 
            messages M, 
            users U, 
            friends F,
            message_share S 
            WHERE 
            F.friend_one='$uid' AND 
            U.uid = F.friend_one AND
            U.status='1' AND 
            F.friend_two != S.ouid_fk AND 
            M.uid_fk = S.ouid_fk AND F.role='fri' AND 
            S.msg_id_fk = M.msg_id group by msg_id)
            UNION
            (SELECT DISTINCT 
            M.msg_id, 
            M.uid_fk, 
            M.message,
            M.share_count,
            U.username,
            U.last_login,
            M.uploads, '0' AS share_uid, '0' AS share_ouid 
            FROM messages M, users U, friends F WHERE F.friend_one='$uid' AND U.status='1' AND M.uid_fk=U.uid AND M.uid_fk = F.friend_two GROUP by msg_id ) ORDER BY created DESC

That's because you are doing an UNION and in that case both part of SELECT should contain exact same number of columns which isn't satisfying for your scenario. Your first SELECT part contains 10 selected columns whereas second SELECT statement contains only 9 columns and so the error.

SELECT DISTINCT 
            M.msg_id, 
            M.uid_fk, 
            M.message,
            S.created, 
            M.share_count, 
            U.username,
            U.last_login,
            M.uploads, 
            S.uid_fk AS 
            share_uid,
            S.ouid_fk AS share_ouid 
            FROM 
            messages M, 
            users U, 
            .........
            UNION
            (SELECT DISTINCT 
            M.msg_id, 
            M.uid_fk, 
            M.message,
            NOW() as created //Add some default value
            M.share_count,
            U.username,
            U.last_login,
            M.uploads, 
            '0' AS share_uid, 
            '0' AS share_ouid 

Both SELECT statements have to contain the same column count (created was missing in the second SELECT statement):

SELECT DISTINCT 
            M.msg_id, 
            M.uid_fk, 
            M.message,
            S.created, 
            M.share_count, 
            U.username,
            U.last_login,
            M.uploads, 
            S.uid_fk AS 
            share_uid,
            S.ouid_fk AS share_ouid
 FROM messages M, users U, friends F, message_share S 
 WHERE F.friend_one='$uid'
 AND U.uid = F.friend_one
 AND U.status='1'
 AND F.friend_two != S.ouid_fk
 AND M.uid_fk = S.ouid_fk
 AND F.role='fri'
 AND S.msg_id_fk = M.msg_id
 GROUP BY msg_id

UNION

SELECT DISTINCT 
            M.msg_id, 
            M.uid_fk, 
            M.message,
            '0000-00-00' as created
            M.share_count,
            U.username,
            U.last_login,
            M.uploads,
            '0' AS share_uid,
            '0' AS share_ouid 
 FROM messages M, users U, friends F
 WHERE F.friend_one='$uid'
 AND U.status='1'
 AND M.uid_fk=U.uid
 AND M.uid_fk = F.friend_two
 GROUP BY msg_id

 ORDER BY created DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM