在 SQL 中的多个值中查找多个值
Look for several values in several value in SQL
这是一个比较特殊的问题:
id | column_num |
---|----------------|
1| 5,12,64,7,78 |
2| 5,53,7,6 |
3| 94,78,32,27 |
我需要找到具有以下值的行:12、7、78
我需要查找对应于单元格 SQL 的多个值。
我使用 MySQL。你有想法吗?
谢谢
好的,我已经得出解决方案,希望对您有所帮助。
Note: You may normalize the query by yourself as I just share my
concept on this problem.
查询如下:-
CREATE TABLE table1
(
id int,
column_num varchar (max)
)
INSERT INTO table1 (id, column_num) VALUES
(1, '5,12,64,7,78'),
(2, '5,53,7,6'),
(3, '94,78,32,27'),
(4, '1,74,32,12,78')
CREATE VIEW vSeperate_Num AS
WITH tmp(id, num, column_num) AS
(
SELECT id, LEFT(column_num, CHARINDEX(',',column_num+',')-1), STUFF(column_num, 1, CHARINDEX(',',column_num+','), '')
FROM table1
UNION ALL
SELECT id, LEFT(column_num, CHARINDEX(',',column_num+',')-1), STUFF(column_num, 1, CHARINDEX(',',column_num+','), '')
FROM tmp
WHERE column_num > ''
)
SELECT id, num
FROM tmp
;WITH Contain_Selected_Num AS
(
SELECT *
FROM
(
SELECT id, num
FROM vSeperate_Num
) AS pvt
PIVOT
(
COUNT(num)
FOR num IN ( [12], [7], [78] )
) AS pvt
)
SELECT *
FROM table1
WHERE id IN
(
SELECT id
FROM Contain_Selected_Num
WHERE [12] > 0 AND [7] > 0 AND [78] > 0
)
谢谢。 :)
至少匹配一个,试试:
select * From table_name where
column_num = '12' or column_num like '%,12,%' or column_num like '%,12' or column_num like '12,%'
union
select * From table_name where
column_num = '7' or column_num like '%,7,%' or column_num like '%,7' or column_num like '7,%'
union
select * From table_name where
column_num = '78' or column_num like '%,78,%' or column_num like '%,78' or column_num like '78,%'
拥有所有三个值,试试这个 SQL:
select * From table_name where (column_num = '12' or column_num like '%,12,%' or column_num like '%,12' or column_num like '12,%')
and (column_num = '7' or column_num like '%,7,%' or column_num like '%,7' or column_num like '7,%')
and (column_num = '78' or column_num like '%,78,%' or column_num like '%,78' or column_num like '78,%')
您可以尝试以下方法:
在每个字符串的开头和结尾添加一个 ,
,即 5,12,64,7,78 -> ,5,12,64,7,78,
然后你可以对每个数字应用MySQL的locate()
函数。
select id, column_num from (
select id, column_num,
locate(',12,', column_num) as m12,
locate(',7,', column_num) as m7,
locate(',78,', column_num) as m78
from your_table
) a
where m12 > 0 and m7 > 0 and m78 > 0;
如果某个数字不在字符串中,则locate的结果为0,否则为字符串中的位置。在外部查询中,您可以在 where
语句中进行检查。
where
语句可以很容易地根据您的需要进行调整,例如如果您需要匹配所有三个数字或至少一个数字。
感谢您的回答。
我必须通过函数 FIND_IN_SET 通过 PHP.
卷曲来找到解决方案
$_POST['test_ids'] = [12,7,78];
if (!empty($_POST['test_ids'])) {
$filter .= ' AND (';
$i = 0;
foreach ($_POST['test_ids'] as $id) {
$filter .= ($i > 0) ? ' OR ' : '';
$filter .= ' FIND_IN_SET(\'' . $id . '\', column_num) ';
$i++;
}
$filter .= ') ';
}
这是一个比较特殊的问题:
id | column_num |
---|----------------|
1| 5,12,64,7,78 |
2| 5,53,7,6 |
3| 94,78,32,27 |
我需要找到具有以下值的行:12、7、78
我需要查找对应于单元格 SQL 的多个值。 我使用 MySQL。你有想法吗?
谢谢
好的,我已经得出解决方案,希望对您有所帮助。
Note: You may normalize the query by yourself as I just share my concept on this problem.
查询如下:-
CREATE TABLE table1
(
id int,
column_num varchar (max)
)
INSERT INTO table1 (id, column_num) VALUES
(1, '5,12,64,7,78'),
(2, '5,53,7,6'),
(3, '94,78,32,27'),
(4, '1,74,32,12,78')
CREATE VIEW vSeperate_Num AS
WITH tmp(id, num, column_num) AS
(
SELECT id, LEFT(column_num, CHARINDEX(',',column_num+',')-1), STUFF(column_num, 1, CHARINDEX(',',column_num+','), '')
FROM table1
UNION ALL
SELECT id, LEFT(column_num, CHARINDEX(',',column_num+',')-1), STUFF(column_num, 1, CHARINDEX(',',column_num+','), '')
FROM tmp
WHERE column_num > ''
)
SELECT id, num
FROM tmp
;WITH Contain_Selected_Num AS
(
SELECT *
FROM
(
SELECT id, num
FROM vSeperate_Num
) AS pvt
PIVOT
(
COUNT(num)
FOR num IN ( [12], [7], [78] )
) AS pvt
)
SELECT *
FROM table1
WHERE id IN
(
SELECT id
FROM Contain_Selected_Num
WHERE [12] > 0 AND [7] > 0 AND [78] > 0
)
谢谢。 :)
至少匹配一个,试试:
select * From table_name where
column_num = '12' or column_num like '%,12,%' or column_num like '%,12' or column_num like '12,%'
union
select * From table_name where
column_num = '7' or column_num like '%,7,%' or column_num like '%,7' or column_num like '7,%'
union
select * From table_name where
column_num = '78' or column_num like '%,78,%' or column_num like '%,78' or column_num like '78,%'
拥有所有三个值,试试这个 SQL:
select * From table_name where (column_num = '12' or column_num like '%,12,%' or column_num like '%,12' or column_num like '12,%')
and (column_num = '7' or column_num like '%,7,%' or column_num like '%,7' or column_num like '7,%')
and (column_num = '78' or column_num like '%,78,%' or column_num like '%,78' or column_num like '78,%')
您可以尝试以下方法:
在每个字符串的开头和结尾添加一个 ,
,即 5,12,64,7,78 -> ,5,12,64,7,78,
然后你可以对每个数字应用MySQL的locate()
函数。
select id, column_num from (
select id, column_num,
locate(',12,', column_num) as m12,
locate(',7,', column_num) as m7,
locate(',78,', column_num) as m78
from your_table
) a
where m12 > 0 and m7 > 0 and m78 > 0;
如果某个数字不在字符串中,则locate的结果为0,否则为字符串中的位置。在外部查询中,您可以在 where
语句中进行检查。
where
语句可以很容易地根据您的需要进行调整,例如如果您需要匹配所有三个数字或至少一个数字。
感谢您的回答。 我必须通过函数 FIND_IN_SET 通过 PHP.
卷曲来找到解决方案$_POST['test_ids'] = [12,7,78];
if (!empty($_POST['test_ids'])) {
$filter .= ' AND (';
$i = 0;
foreach ($_POST['test_ids'] as $id) {
$filter .= ($i > 0) ? ' OR ' : '';
$filter .= ' FIND_IN_SET(\'' . $id . '\', column_num) ';
$i++;
}
$filter .= ') ';
}