2

我在 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

我明白了

在此处输入图像描述

为什么我NULL得到rownum

其次,我需要按该 ID 对记录进行分组

GROUP BY rownum;

谁能帮我?

4

4 回答 4

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
于 2012-09-25T08:44:25.867 回答
0

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

 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
于 2012-09-25T08:27:55.437 回答
0
  1. 在运行查询之前将 rownum 设置为 0。
  2. 按 rownum 分组不会有任何区别 - 每行的 rownum 都会不同。
于 2012-09-25T08:28:04.223 回答
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;
于 2012-09-25T08:28:19.773 回答