使用@rownum枚举SQL中的每条记录:= @ rownum + 1 AS rownum

时间:2012-09-25 08:25:49

标签: mysql sql mysqli

我在MySQL中有这个查询

SELECT 
@rownum:=@rownum+1 AS rownum,
notifications_posts.from_user AS source,
notifications_posts.on_post_id AS destination,
notifications_posts.in_group_id,
groups.group_name AS group_name,
notifications_posts.on_post_id AS other_record_id,
user_info.first_name,
user_info.last_name,
user_info.user_id,
notifications_posts.date,
posts.title AS 'title',
user_rights.right AS 'right',
'article' AS notification_type
FROM notifications_posts
INNER
    JOIN user_info
    ON notifications_posts.from_user = user_info.user_id
INNER
    JOIN posts
    ON posts.id = notifications_posts.on_post_id
INNER
    JOIN groups
    ON groups.group_id = notifications_posts.in_group_id
INNER
    JOIN user_rights
    ON user_rights.group_id = notifications_posts.in_group_id AND user_rights.user_id = user_info.user_id

我得到了

enter image description here

为什么我在NULL获得rownum

其次,我需要按照该ID

对记录进行分组
GROUP BY rownum;

任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:1)

我认为最好只订购结果(在MySQL中),然后在应用程序中添加行号。如果你坚持:

警告:任何以这种方式使用变量的查询都可能在未来的MySQL版本中被破坏。

SELECT 
    @rownum := @rownum+1 AS rownum,
    notifications_posts.from_user AS source,
    notifications_posts.on_post_id AS destination,
    notifications_posts.in_group_id,
    groups.group_name AS group_name,
    notifications_posts.on_post_id AS other_record_id,
    user_info.first_name,
    user_info.last_name,
    user_info.user_id,
    notifications_posts.date,
    posts.title AS 'title',
    user_rights.right AS 'right',
    'article' AS notification_type
FROM 
    (SELECT @rownum:=0) AS dummy                  -- initial value
CROSS 
    JOIN notifications_posts
INNER
    JOIN user_info
    ON notifications_posts.from_user = user_info.user_id
INNER
    JOIN posts
    ON posts.id = notifications_posts.on_post_id
INNER
    JOIN groups
    ON groups.group_id = notifications_posts.in_group_id
INNER
    JOIN user_rights
    ON user_rights.group_id = notifications_posts.in_group_id 
    AND user_rights.user_id = user_info.user_id 
ORDER BY                                         -- you need 
    whatever ;                                   -- ORDER BY clause

答案 1 :(得分:0)

你必须使用语句初始化变量@rownum(SELECT rownum:= 0)r

 select  @rownum:=@rownum+1 AS rownum,a.*
     from
     (SELECT 
       notifications_posts.from_user AS source,
       notifications_posts.on_post_id AS destination,
       notifications_posts.in_group_id,
       groups.group_name AS group_name,
       ........
       <remaining query>
       )a,(SELECT @rownum:=0) r 

编辑:所以您的完整查询应该是

SELECT 
@rownum:=@rownum+1 AS rownum,a.*
FROM
(
    SELECT
    notifications_posts.from_user AS source,
    notifications_posts.on_post_id AS destination,
    notifications_posts.in_group_id,
    groups.group_name AS group_name,
    notifications_posts.on_post_id AS other_record_id,
    user_info.first_name,
    user_info.last_name,
    user_info.user_id,
    notifications_posts.date,
    posts.title AS 'title',
    user_rights.right AS 'right',
    'article' AS notification_type
    FROM notifications_posts
    INNER
        JOIN user_info
        ON notifications_posts.from_user = user_info.user_id
    INNER
        JOIN posts
        ON posts.id = notifications_posts.on_post_id
    INNER
        JOIN groups
        ON groups.group_id = notifications_posts.in_group_id
    INNER
        JOIN user_rights
        ON user_rights.group_id = notifications_posts.in_group_id AND user_rights.user_id = user_info.user_id
)a, (SELECT @rownum:=0 )r

答案 2 :(得分:0)

  1. 在运行查询之前将rownum设置为0.
  2. 按rownum分组不会有任何区别 - 每行的rownum会有所不同。

答案 3 :(得分:0)

SELECT @rownum:=0;
SELECT 
@rownum:=@rownum+1 AS rownum,
notifications_posts.from_user AS source,
notifications_posts.on_post_id AS destination,
notifications_posts.in_group_id,
groups.group_name AS group_name,
notifications_posts.on_post_id AS other_record_id,
user_info.first_name,
user_info.last_name,
user_info.user_id,
notifications_posts.date,
posts.title AS 'title',
user_rights.right AS 'right',
'article' AS notification_type
FROM notifications_posts
INNER
    JOIN user_info
    ON notifications_posts.from_user = user_info.user_id
INNER
    JOIN posts
    ON posts.id = notifications_posts.on_post_id
INNER
    JOIN groups
    ON groups.group_id = notifications_posts.in_group_id
INNER
    JOIN user_rights
    ON user_rights.group_id = notifications_posts.in_group_id AND user_rights.user_id = user_info.user_id;