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 に答える