我使用以下代码获取此信息:
使用的SELECT语句具有不同的列数。我仍然从这个网站搜索解决方案,但我找不到我的查询中的问题。任何人都可以在这里帮助我为什么我得到= "使用过的SELECT语句有不同数量的列" 以及如何解决它?
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
答案 0 :(得分:2)
这是因为您正在执行UNION
,在这种情况下,SELECT
的两个部分都应包含完全相同的列数,这些列并不适合您的方案。您的第一个SELECT
部分包含10个选定列,而第二个SELECT
语句仅包含9个列,因此错误。
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
答案 1 :(得分:1)
两个SELECT语句都必须包含相同的列计数(在第二个SELECT语句中缺少创建):
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