从联合mysql查询中获取帖子的数量?

时间:2010-10-16 00:21:03

标签: sql mysql

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我试过

所以我做了什么

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类计数。

2 个答案:

答案 0 :(得分:1)

COUNT函数不允许您拥有多个列。它可以是COUNT(*)或COUNT(column_name),其中只有一个列名。 COUNT(*)格式计算总行数,其中COUNT(column_name)返回指定列的非空值的计数。

因此,下一步是在SELECT中更改COUNT。然后,如果存在其他问题,您可以从那里开始。

答案 1 :(得分:0)

php中的mysql_num_rows似乎在没有修改sql的情况下将其拉下来。