开发者

Better way to write this SQL in Python

开发者 https://www.devze.com 2023-03-03 17:10 出处:网络
Is there anyway I can reduce the duplication of my arguments? For example, timepattern is repeated 4 times and it gets hard to keep track of queries that are big.

Is there anyway I can reduce the duplication of my arguments? For example, timepattern is repeated 4 times and it gets hard to keep track of queries that are big.

    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() 


try with this with using args as a parameters dictionnary and using explicit string interpolation args (ie %(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)


You can give args as dict and then use it in query like %(name)s For example

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号