2012-09-25 57 views
2

我在MySQL枚举SQL每条记录与@rownum:= @ ROWNUM + 1的rownum

SELECT 
@rownum:[email protected]+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

为什么我得到NULLrownum

和所有第二,我通过ID需要组记录

GROUP BY rownum; 

谁能帮助我?

+0

MySQL的,对不起...... – Empeus

回答

0

你与语句来初始化变量@rownum(SELECT ROWNUM:= 0)R

select @rownum:[email protected]+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 

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

​​
+0

又是你?谢谢,但我已经做了'(SELECT rownum:= 0)r'的东西,并得到了#1064 - 你的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以在':= 0'附近使用正确的语法'r INNER JOIN user_info ON notifications_posts.from_user = user_inf'at line 15' – Empeus

+0

@Empeus:我已经更新了我的答案,请现在检查 –

+0

尝试(选择@rownum:= 0)而不是 – podiluska

0
  1. 套装在运行查询之前将rownum设置为0。
  2. rownum的分组不会有什么区别 - rownum每行都会有所不同。
+0

好吧,但这不是一个存储过程,这是一个简单的查询从PHP ... – Empeus

0
SELECT @rownum:=0; 
SELECT 
@rownum:[email protected]+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; 
+0

这是什么'user_info.user_id(@rownum:= 0)'???与'user_info.user_id'有什么关系? – Empeus

+0

得到'#1305 - 功能user_info.user_id不存在'上那个问题... – Empeus

+0

已更新查询请现在尝试 – AnandPhadke

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 
+0

这工作,但它会工作在一个更大的查询?这是实际查询http://codepad.org/ZCYrlXwh – Empeus

+0

通知,有多个选择 – Empeus

+0

取决于你想要的数字。每一行都有不同的行号?或者每个联盟部分的编号应该从1重新开始? –