如何将一行Mysql显示为多行?
我需要一些帮助。 我有一个简单的mysql查询:如何将一行Mysql显示为多行?,mysql,Mysql,我需要一些帮助。 我有一个简单的mysql查询: select '1,2,3,4,5,6' as Col1 from dual; 表1: Col1 1,2,3,4,5,6 我还有另一张表2: service_id service_name 1 Service1 2 Service2 我尝试了下一个查询,但不起作用: select service_name from table2 where service_id in (select c
select '1,2,3,4,5,6' as Col1 from dual;
表1:
Col1
1,2,3,4,5,6
我还有另一张表2:
service_id service_name
1 Service1
2 Service2
我尝试了下一个查询,但不起作用:
select service_name from table2 where service_id in (select col1 from table1)
任何帮助都将不胜感激 试试这个:
样本数据:
create table `dual` (Col1 varchar(100));
insert into `dual` values ('1,2,3,4,5,6');
create table table2 (service_id int, service_name varchar(100));
insert into table2 values
(1, 'Service1'),
(2, 'Service2');
T-SQL:
select service_name from table2 t2
where exists(
select 1 from `dual` d
where locate(concat(',', t2.service_id, ','), concat(',', d.Col1, ',')) > 0
);
我不确定你到底想完成什么,但我可以给你一些提示 您的IN条款仅在您的服务id为“1,2,3,4,5,6”时有效。我可以看到您试图做什么,但数据库将col1的结果视为整个数字字符串,包括所有逗号,而不是单个数字本身。如果您将其硬编码为1,2,3,4,5,6中的WHERE,您将获得匹配项。您可以连接表并使用LIKE,而不是使用IN子句 试试这个:
SELECT
table2.service_name
FROM
table2
LEFT OUTER JOIN
table1
ON
table1.col1 LIKE CONCAT('%', table2.service_id, '%')
WHERE
table1.col1 IS NOT NULL
我想这会达到我认为您想要的效果。您确定这是吗?使用dual似乎表明您正在使用。是的!它也在mysql中工作…mysql有一个find_in_set函数,可以为您完成这项工作。非常感谢您的支持!它起作用了!这就是我需要的!非常感谢!