我不认为有必要尝试使用FIND_IN_SET()
的多个或条件的解决方案,其中myColumn
可能包含随机的1,2,3
或2,3,6..
数据FIND_IN_SET( '1', myColumn ) OR FIND_IN_SET( '3', myColumn ) OR FIND_IN_SET( '5', myColumn )
如果这不是正确的,我可能需要一个标准化的解决方案来满足我的查询
最佳答案
如果可能的话,使数据库正常化,但是如果你绝望了,你可以这样做:-
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)";
?>