1

无论如何我可以减少我的论点的重复吗?例如,timepattern 重复了 4 次,很难跟踪大查询。

    sql = ( "SELECT IFNULL(b.inviters/COUNT(DISTINCT c.id),0),                                  " 
            "       FROM_UNIXTIME(c.registered_at, %s)                                          "
            "FROM   (   SELECT COUNT(1) AS inviters, joindate                                   "
            "           FROM                                                                    "
            "               (   SELECT  DISTINCT(y.id) AS inviters,                             " 
            "                           FROM_UNIXTIME(y.registered_at, %s) AS joindate          "
            "                   FROM user_invites z                                             "
            "                   INNER JOIN users y ON y.id = z.inviter_id                       "
            "                   WHERE z.created_at >= %s                                        "
            "               ) a                                                                 "
            "           GROUP BY a.joindate                                                     "
            "       ) b                                                                         "   
            "INNER JOIN users c ON FROM_UNIXTIME(c.registered_at, %s) = b.joindate              "
            "WHERE c.registered_at BETWEEN %s AND %s                                            "
            "GROUP BY FROM_UNIXTIME(c.registered_at, %s)                                        "   )

    args =  (   timepattern, timepattern, datestart_int, timepattern,
                datestart_int, dateend_int, timepattern )   

    cursor.execut(sql, args)

    data = list(cursor.fetchall())

    cursor.close()
    connection.close() 
4

2 回答 2

5

尝试使用 args 作为参数字典并使用显式字符串插值 args(即%(parameter_name)s):

sql = ( "SELECT IFNULL(b.inviters/COUNT(DISTINCT c.id),0),                                  " 
            "       FROM_UNIXTIME(c.registered_at, %(timepattern)s)                                          "
            "FROM   (   SELECT COUNT(1) AS inviters, joindate                                   "
            "           FROM                                                                    "
            "               (   SELECT  DISTINCT(y.id) AS inviters,                             " 
            "                           FROM_UNIXTIME(y.registered_at, %(timepattern)s) AS joindate          "
            "                   FROM user_invites z                                             "
            "                   INNER JOIN users y ON y.id = z.inviter_id                       "
            "                   WHERE z.created_at >= %(datestart_int)s                                        "
            "               ) a                                                                 "
            "           GROUP BY a.joindate                                                     "
            "       ) b                                                                         "   
            "INNER JOIN users c ON FROM_UNIXTIME(c.registered_at, %(timepattern)s) = b.joindate              "
            "WHERE c.registered_at BETWEEN %(datestart_int)s AND %(dateend_int)s                                            "
            "GROUP BY FROM_UNIXTIME(c.registered_at, %(timepattern)s)                                        "   )

    args =  {
        "timepattern" : timepattern,
        "datestart_int" : datestart_int,
        "dateend_int" : dateend_int,
    }   

    cursor.execute(sql, args)
于 2011-05-09T08:57:19.793 回答
4

您可以将 args 作为 dict 提供,然后在查询中使用它,%(name)s例如

args = {'timepattern': timepattern, 'dateend_int': dateend_int}
sql = ( "SELECT IFNULL(b.inviters/COUNT(DISTINCT c.id),0),                                  " 
            "       FROM_UNIXTIME(c.registered_at, %(timepattern)s)                                          "
            "FROM   (   SELECT COUNT(1) AS inviters, joindate                                   "
            "           FROM                                                                    "
            "               (   SELECT  DISTINCT(y.id) AS inviters,                             " 
            "                           FROM_UNIXTIME(y.registered_at, %(timepattern)s AS joindate          "
            "                   FROM user_invites z                                             "
            "                   INNER JOIN users y ON y.id = z.inviter_id                       "
            "                   WHERE z.created_at >= %(datestart_int)s                                        "
            "               ) a                                                                 "
            "           GROUP BY a.joindate                                                     "
            "       ) b                                                                         "
"INNER JOIN users c ON FROM_UNIXTIME(c.registered_at, %(timepattern)s) = b.joindate " "WHERE c.registered_at BETWEEN %(datestart_int)s AND %(datetart_int)s " "GROUP BY FROM_UNIXTIME(c.registered_at, %(timepattern)s)" ) cursor.execute(sql, args)

于 2011-05-09T08:48:14.427 回答