我认为使用FIND_IN_SET()
尝试使用多个OR条件的解决方案是不值得的,其中myColumn
可能包含随机数1,2,3
或2,3,6..
FIND_IN_SET( '1', myColumn ) OR FIND_IN_SET( '3', myColumn ) OR FIND_IN_SET( '5', myColumn )
如果这不是正确的,我可能需要一个符合我的查询的标准化解决方案
答案 0 :(得分:3)
如果可能的话,将数据库标准化,但如果你绝望,你可以这样做: -
SELECT DISTINCT Sometable.*
FROM SomeTable
INNER JOIN
(
SELECT 1 AS i UNION SELECT 2 UNION SELECT 3
) Sub1
WHERE FIND_IN_SET(Sub1.i, Sometable.myColumn)
编辑 - 使用没有循环的键数组
<?php
$some_keys = array(1,2,3);
$sql = "SELECT DISTINCT Sometable.*
FROM SomeTable
INNER JOIN
(
SELECT ".implode(" AS i UNION SELECT ", $some_keys)." AS i
) Sub1
WHERE FIND_IN_SET(Sub1.i, Sometable.myColumn)";
?>
或者,如果值适用于另一个表的键,则可以使用: -
<?php
$some_keys = array(1,2,3);
$sql = "SELECT DISTINCT Sometable.*
FROM SomeTable
INNER JOIN SomeOtherTable
ON SomeOtherTable.id IN (".implode(",", $some_keys).")
AND FIND_IN_SET(SomeOtherTable.id, Sometable.myColumn)";
?>