SQL查询在MariaDB控制台中有效,但在python pymysql

时间:2018-04-04 03:07:44

标签: python mysql mariadb pymysql

我的SQL查询在 MariaDB CLI中有效,但在python pymysql 中无效。

我的要求:

我想获取具有相同用户名的记录编号( COUNT(*)),然后为每个用户获取排名(rowNo),如果是用户名具有相同的记录( COUNT(*)),等级(rowNo)也相同(@rowNo:= @ rowNo)。

问题:

在SQL下 MariaDB CLI中,相同数量的记录具有相同的排名(rowNo)。但是在python pymysql中,相同数量的reocord有不同的等级(rowNO),它会增加(@rowNo:= @ rowNo + 1)。为什么呢?

        art_sql = "SELECT T2.rowNo, T2.username, T2.art_number                        \
               FROM                                                               \
               (SELECT T1.art_number,                                             \
                       T1.username,                                               \
                       IF(@c=T1.art_number,@rowNo:=@rowNo,@rowNo:=@rowNo+1) AS rowNo, \
                       @c:=T1.art_number                                          \
               FROM                                                               \
                 (                                                                \
                   SELECT username,COUNT(*) AS art_number                         \
                   FROM %s                                                        \
                   GROUP BY username                                              \
                   ORDER BY art_number DESC                                       \
                 )T1                                                              \
               )T2, (SELECT @rowNo:=0,@c:=0)T3                                    \
               WHERE T2.username='%s';"                                           \
               % (self.__article_table, username)
顺便说一下,我在下面有一个类似的SQL没有这个问题:

        rev_sql = "SELECT T2.rowNo, T2.username, T2.total_revenue                     \
               FROM                                                               \
               (SELECT T1.total_revenue,                                          \
                       T1.username,                                               \
                       IF(@c=T1.total_revenue,@rowNo:=@rowNo,@rowNo:=@rowNo+1) AS rowNo, \
                       @c:=T1.total_revenue                                       \
               FROM                                                               \
                 (                                                                \
                   SELECT username,SUM(money) AS total_revenue                    \
                   FROM %s                                                        \
                   GROUP BY username                                              \
                   ORDER BY total_revenue DESC                                    \
                 )T1                                                              \
               )T2, (SELECT @rowNo:=0,@c:=0)T3                                    \
               WHERE username='%s';"                                              \
               % (self.__article_table, username)

你能帮忙解答一下吗?感谢〜

1 个答案:

答案 0 :(得分:0)

IF(@ c = T1.total_revenue,@ rowNo:= @ rowNo,@ rowNo:= @ rowNo + 1)AS行号,\                        @c:= T1.total_revenue 尝试  IF(@ c = T1.total_revenue,@ rowNo:= @ rowNo,@ rowNo:= @ rowNo + 1)AS行号,\                        (@c:= T1.total_revenue)