用 Python 编写此 SQL 的更好方法

发布于 2024-11-05 17:40:14 字数 1800 浏览 1 评论 0原文

无论如何我可以减少我的论点的重复吗?例如,时间模式重复 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() 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖个坑埋了你 2024-11-12 17:40:14

尝试使用 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)

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)
逆光飞翔i 2024-11-12 17:40:14

您可以将 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)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文