2

I don't think it is worth to try for a solution with multiple OR conditions using FIND_IN_SET() where myColumn might contain data as 1,2,3 or 2,3,6.. in random FIND_IN_SET( '1', myColumn ) OR FIND_IN_SET( '3', myColumn ) OR FIND_IN_SET( '5', myColumn )

If this is not the right one, I might require a normalised solution to suit my query

4

1 回答 1

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)";

?>
于 2014-02-25T09:43:43.253 回答