gpt4 book ai didi

mysql - 从mysql中逗号分隔的列中搜索多个值

转载 作者:行者123 更新时间:2023-11-29 01:58:17 25 4
gpt4 key购买 nike

我不认为有必要尝试使用FIND_IN_SET()的多个或条件的解决方案,其中myColumn可能包含随机的1,2,32,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)";

?>

关于mysql - 从mysql中逗号分隔的列中搜索多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22007502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com