我有两个表:gem和gemdetail保持连接状态。我试图将LEFT& Y' \. h! M/ p+ _
JOIN限制为gems表中的10条记录。也有两个其他表联接(gemreply和用户),但它们不会导致该问题。以下内容不起作用: 2 |% ^# {% e: ^8 U3 ]SELECT gems.gemid, gems.title, r.tot, gemdetail.filename FROM ((gems $ Q/ N1 J, ~1 O! p( Z) y
LEFT JOIN (SELECT gemid, COUNT(*) AS tot FROM gemreply GROUP BY gemid) AS r ON gems.gemid = r.gemid) . f9 R- R) t* T
LEFT JOIN gemdetail ON gems.gemid = gemdetail.gemid) 0 l; F" u6 l1 @3 f7 `, ^* b
LEFT JOIN users ON gems.userid = users.userid # ]: r* [1 f8 u6 Q
WHERE gems.grade = '7' ORDER BY gems.gemid LIMIT 0, 10; ' |+ Q; M) f8 ]; s& a5 b这将返回的行总数限制为10,但是由于每个gem有多个明细记录,所以我剩下不到10个gem记录。我已经阅读了每个“ LIMIT”帖子,但没有发现这种情况。, t y- |2 p$ d& L: L! v
更新1:好的-感谢jviladrich-它奏效了。这是代码: - ` x2 }2 W9 [* z. T) y# R' a' oSELECT gems.gemid, gems.title, r.tot, gemdetail.filename * Z+ F2 r0 C- w% ]
FROM ((gems 1 w; d9 V* [# e1 l) H0 k% P% IINNER JOIN (SELECT gems.gemid from gems WHERE gems.grade = '7' ORDER BY gems.gemid LIMIT 0, 10) g+ P: f$ K' L7 H! j. y0 f
ON (gems.gemid = g.gemid) ' N2 a8 v4 W7 nLEFT JOIN (SELECT gemid, COUNT(*) AS tot FROM gemreply GROUP BY gemid) AS r ON gems.gemid = r.gemid) 8 N; W& C; R7 K! u7 M& M* e" _LEFT JOIN gemdetail ON gems.gemid = gemdetail.gemid) " B m, f5 _2 l: R+ w# C( Z7 DLEFT JOIN users ON gems.userid = users.userid ; ( j! ^; ~6 \; _$ ?; x更新2:yogeshr下面的代码也可以工作-可能是我将要使用的代码。感谢你们俩!9 `# H( z( j4 Y' j, D# Z5 ?
/ c% E1 ` X9 Q. k+ D+ D解决方案:; M3 {7 b( r2 a& e5 l7 A) W3 m+ L
$ |7 R* a# ~# |- l) m: [" Q # j$ z" N& { C2 i* ?$ k# C! ^4 N/ n, |' K% k
像这样 + l* ]9 ?. X3 g SELECT * FROM A * b* r( `! K; ^( l& q: a INNER JOIN ( SELECT * FROM A WHERE A.FIELD1='X' ORDER BY A.FIELD2 LIMIT 10) X ( K( L- d0 O8 B. }: j0 C ON (A.KEYFIELD=X.KEYFIELD) 7 j! w C f& Z6 R5 j LEFT JOIN B ON (A.FIELD = B.FIELD) ' m. ~, ]2 {1 T9 C! B LEFT JOIN C ON (A.FIELD = C.FIELD)