我有一個像這樣的MySQL(5.7)表。MySQL:用單個語句搜索多個列
+--------+--------------+--------+-------+----------+--------+
| Field | Type | Null | Key | Default | Extra |
|--------+--------------+--------+-------+----------+--------|
| colA | varchar(50) | NO | PRI | <null> | |
| colB | int(11) | YES | | <null> | |
| colC | int(11) | YES | | <null> | |
| colD | int(11) | YES | | <null> | |
| colE | int(11) | YES | | <null> | |
| colF | int(11) | YES | | <null> | |
| colG | int(11) | YES | | <null> | |
| colH | int(11) | YES | | <null> | |
| colI | int(11) | YES | | <null> | |
| colJ | int(11) | YES | | <null> | |
| colK | int(11) | YES | | <null> | |
| colL | int(11) | YES | | <null> | |
| colM | int(11) | YES | | <null> | |
我想搜索所有colB,colC,...,colM mod 100是1或2.我知道我可以這樣做。
SELECT * FROM MYTABLE WHERE
(mod(colB, 100) = 1 OR mod(colB, 100) = 2) AND
(mod(colC, 100) = 1 OR mod(colC, 100) = 2) AND
(mod(colD, 100) = 1 OR mod(colD, 100) = 2) AND
:
(mod(colM, 100) = 1 OR mod(colM, 100) = 2)
該SQL非常長且效率低下。我能做得更聰明嗎?
爲什麼你有這麼多列在第一位? –