查询SELECT语句的列数不同 [英] query SELECT statements have a different number of columns

查看:69
本文介绍了查询SELECT语句的列数不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下代码获取此信息:

I am getting this with the following code:

使用的SELECT语句具有不同数量的列.我仍然从该站点搜索解决方案,但在查询中找不到问题所在.任何人都可以在这里为我提供帮助,为什么我得到= 使用的SELECT语句的列数不同" ,我该如何解决?

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

推荐答案

这是因为您正在执行UNION,在这种情况下,SELECT的两个部分都应包含完全相同的列数,这不满足您的要求设想.您的第一个SELECT部分包含10个选定的列,而第二个SELECT语句仅包含9个列,因此是错误的.

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 

这篇关于查询SELECT语句的列数不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆