示例:SELECT title,ROW_NUM FROM article ORDER BY count_read。 ROW_NUM应该被替换为什么?
我不喜欢在得到结果后按程序生成索引,因为我想通过查询上面的示例DQL将结果数据插入表Rank中。
我想要实现的目标可能是:
"INSERT INTO RANK r (title, index, lastIndex)
SELECT title,ROW_NUM,(SELECT index FROM RANK WHERE id = :id - 1) FROM article ORDER BY count_read"
提前致谢..
答案 0 :(得分:0)
我认为您可能会使用变量,如下所示:
"
SET @row_num := 1;
INSERT INTO RANK r (title, index, lastIndex)
SELECT title,
(@row_num := @row_num + 1),
(SELECT index FROM RANK WHERE id = :id - 1)
FROM article ORDER BY count_read
"