2017-06-07 78 views
1

我有以下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工作臺,看起來像我有一個語法錯誤,看起來像%%是無效的。

+0

https://stackoverflow.com/a/8616880/8106112 –

+0

感謝@darnell,我已經看到了這一個,但我不完全確定它是相同的,因爲它是像「%lastuser%」之類的東西。 .. –

+2

不要使用Python字符串格式來生成SQL查詢字符串;使用你的圖書館的能力來創建參數化查詢。 – chepner

回答

1

第一個是Modulo操作並計算id除以partition_size的餘數,然後將其與節點進行比較。

在這裏看看一個簡單的例子:

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

More function_mod examples

正如你注意到%%是不是一個有效的操作。