【问题标题】:What do %% and % in these queries mean?这些查询中的 %% 和 % 是什么意思?
【发布时间】:2017-06-07 00:16:34
【问题描述】:

我有以下 2 个 Python 代码:

def direct_by_node(self, node, partition_size):
    return '''select req.id, req.profile_id as profile
                  from table1 attr
                  where id % {0} = {1}'''.format(partition_size, node)

然后

def find_by_node(self, node, partition_size):
    return '''select req.id, req.profile_id as profile
                  from table1 attr
                  where id %% {0} = {1}'''.format(partition_size, node)

我想知道 2 个 MySQL 查询中的 % 和 %% 是关于什么的?

编辑:

当我在 MySQL Workbench 中尝试 %% 时,看起来我有语法错误,看起来 %% 无效。

【问题讨论】:

  • 感谢@darnell,我看过这个,但我不完全确定它是否与我的相同,因为它类似于“%lastuser%”...
  • 不要使用Python字符串格式化生成SQL查询字符串;使用您的库创建参数化查询的能力。

标签: python mysql


【解决方案1】:

第一个是Modulo运算,计算id除以partition_size的余数,然后与node比较。

看一个简单的例子:

mysql> SELECT 253 % 7;
    -> 1 # because 253 = 36*7 + 1

More function_mod examples

您注意到 %% 不是有效的运算符。

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2013-05-09
    • 2012-09-14
    • 2023-02-14
    • 1970-01-01
    • 2014-09-14
    相关资源
    最近更新 更多