|
为什么我得到#1222-使用的SELECT语句具有不同数量的列?我正在尝试从此用户朋友和他自己处加载墙贴。
: d" b# I/ T# [SELECT u.id AS pid, b2.id AS id, b2.message AS message, b2.date AS date FROM + E2 I5 q6 L; Q# ^- D/ e4 Q
() a; N+ I% W" v9 `6 Z( o
(
3 K: S' D/ ^& {5 P$ X8 ?1 h) T SELECT b.id AS id, b.pid AS pid, b.message AS message, b.date AS date FROM 1 e( v2 Q. [% L" T) E
wall_posts AS b ) q' c2 h& K( d) y; ~% m2 ?% D5 e
JOIN Friends AS f ON f.id = b.pid
9 U6 p( i/ }8 ~! u WHERE f.buddy_id = '1' AND f.status = 'b'
0 b: O7 F- L( O' W ORDER BY date DESC Y. x8 l. v: Y! V) x' h
LIMIT 0, 10) H" O, S9 S) @# v, l
)
5 u3 t' f, m% e- ^7 ^6 ~9 W UNION
9 X' N2 o6 l. a( S6 r/ F (
: \2 `$ w& ]6 f3 J: y2 Q, ] SELECT * FROM
4 M* J% T' n1 q2 a A- T" e wall_posts, Q4 |# r, i& i9 c8 f; G) C- a
WHERE pid = '1'
) p6 |3 [& W8 H" J: s$ |; a3 b7 U+ ?. B ORDER BY date DESC) A) R0 x; i9 z0 i
LIMIT 0, 10
& q. q! e# h' S- Y )
6 o Y' |/ N; @( \, {! t ORDER BY date DESC4 N4 d8 ]$ `0 m* t" ]" B7 M' h
LIMIT 0, 10! o* W: @; `+ N# o/ Y# n+ [& N) G% p/ p
) AS b2
/ s* V" H+ E8 E; vJOIN Users AS u8 s! Z( S6 O* |! c
ON b2.pid = u.id5 a! ~! V- l6 `2 w& F- x3 a. w
WHERE u.banned='0' AND u.email_activated='1'
' W* c. j* @3 |* sORDER BY date DESC
. m1 {* C+ K* q* }LIMIT 0, 10
" ], j8 l4 g% lwall_posts表结构如下所示 id date privacy pid uid message# K1 }3 \& D& `9 g
Friends表结构如下所示 Fid id buddy_id invite_up_date status& f# y6 K7 \) E" A7 |- f
pid代表配置文件ID。我不太确定发生了什么。" q( d$ M$ G4 s
( y% x2 ?" I: i9 [; f H I解决方案:
! G5 Q( l1 X+ r- S! e 6 L6 h/ i+ l# G' d
( B$ k ^6 z# ]1 R2 ?
& g& B6 y7 ?" G5 \3 X
UNION中的第一条语句返回四列:# N4 i$ Q! _3 f
SELECT b.id AS id,
% K7 Y( ~4 k3 U- W b.pid AS pid, + [. R# Q" V& o2 z, s/ M5 E+ X. W! Q
b.message AS message,
4 \) F- V3 z% l k b.date AS date
4 }9 w' t: `3 ^) T- F FROM wall_posts AS b" f6 M4 Y2 z0 ^2 j# C" E& a
第二个返回 六 ,因为*扩展为包括以下所有列WALL_POSTS:
0 w% V( y% t( E* A" A0 G: ?+ cSELECT b.id,
+ i+ X! m3 B: J5 Y b.date, % J; y/ D* W, o, j
b.privacy,% W7 Z$ I1 t5 U# F
b.pid. : L9 D$ K) m, G. a1 Q0 P
b.uid message' A/ E" w% K+ _# j/ N- K
FROM wall_posts AS b, L9 j2 [8 M. k' j' b- [
在UNION与UNION ALL运营商要求:, z4 g$ f( ~2 e
[ol]组成UNION查询的所有语句中存在相同数量的列0 o! e: c. x- r Q6 F) M1 j
数据类型必须在每个位置/列都匹配
! r0 p& y' t. ^- S& n1 Z3 N* f1 i[/ol]- ~9 S( ^! z( |. r
使用:4 c0 f' H! N9 n* m+ s
FROM ((SELECT b.id AS id, 8 U, c/ p" C# E/ W% b/ ]6 n
b.pid AS pid, # h' w# r9 h k
b.message AS message,
, ^2 o0 a, O6 i6 D0 d! v: V b.date AS date
& Q% I- t, {4 ~3 B) z Q FROM wall_posts AS b
6 F/ M# r/ b' b" h0 q% p7 t" q JOIN Friends AS f ON f.id = b.pid + j$ {. @# H$ H6 W$ Y2 O3 O7 E" `3 x( ?
WHERE f.buddy_id = '1' AND f.status = 'b'/ F9 d9 B! r" S! v1 ]5 q
ORDER BY date DESC
Q& y( ^5 i/ y6 c8 f LIMIT 0, 10)" O+ K& E0 B$ _. \8 s
UNION
! w$ k. r( J$ g7 @. O* S* E (SELECT id,
( O7 W* W% N/ @4 F# l( ~- W; z# Y pid,
& S! Z& Y4 r& `: N0 F u9 B message,
" ?% g; D! ~6 p* b" q% } date7 H k0 H$ J0 T+ {' m5 S
FROM wall_posts
" m/ f: X3 U' ?1 b1 G. p/ M WHERE pid = '1', _5 V1 b. J1 U# L6 }: N
ORDER BY date DESC) s2 b6 w, c4 u; v# E. {
LIMIT 0, 10)) |
|