2010-10-16 44 views
1
SELECT u.id  AS pid , 
     b2.id  AS id  , 
     b2.message AS MESSAGE, 
     b2.uid AS uid, 
     b2.date AS DATE 
FROM (
     (SELECT b.id   AS id  , 
       b.pid  AS pid , 
       b.message AS MESSAGE, 
       b.uid AS uid, 
       b.date  AS DATE 
     FROM  wall_posts AS b 
       JOIN Friends AS f 
       ON  f.id = b.pid 
     WHERE f.buddy_id = '1' 
     AND  f.status  = 'b' 
     ORDER BY DATE DESC 
     LIMIT 0, 10 
     ) 

UNION 
     (SELECT id  , 
        pid , 
        MESSAGE, 
        uid, 
        DATE 
     FROM  wall_posts 
     WHERE pid = '1' 
     ORDER BY DATE DESC 
     LIMIT 0, 10 
     ) 
     )   AS b2 
     JOIN Users AS u 
     ON  b2.pid = u.id 
WHERE u.banned   ='0' 
AND  u.email_activated='1' 
ORDER BY DATE DESC 
LIMIT 0, 10 

是代碼。不知道我怎麼會得到這個帖子。平時我知道我會做select count(*) as num我試圖從聯合mysql查詢獲得帖子的數量?

所以我做了什麼我把

SELECT u.id  AS pid , 
     b2.id  AS id  , 
     b2.message AS MESSAGE, 
     b2.uid AS uid, 
     b2.date AS DATE 
FROM 

,並把它改成

SELECT COUNT(u.id  AS pid , 
     b2.id  AS id  , 
     b2.message AS MESSAGE, 
     b2.uid AS uid, 
     b2.date AS DATE) as num 
FROM 

,並沒有類似的東西所有的select語句,清楚,沒有工作,不斷得到錯誤,如#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS pid , b2.id AS id , b2.message AS MESSAGE, b' at line 1。那麼我怎麼會得到伯爵呢?我需要計數我的分頁php類。

回答

0

php中的mysql_num_rows似乎將其關閉而不修改sql。

1

COUNT函數不允許您有多個列。它可以是COUNT(*)或COUNT(column_name),其中只有一個列名。 COUNT(*)格式計算總行數,其中COUNT(column_name)返回指定列的非空值的計數。

因此,下一步是在SELECT中改變COUNT。然後,如果存在其他問題,您可以從那裏出發。

+0

Nvm,我剛剛發現mysql_num_rows同時做。 – Keverw 2010-10-16 00:46:53