结果1来自table1:
select types from table1 where id = 123456;结果:
934046, 934049然后,将结果1作为table2的参数:
select GROUP_CONCAT(name) from table2 where id in (
select types from table1 where id = 123456
);但是,结果1是一个字符串值,所以结果:
NULL事实上,我想要的是:
select GROUP_CONCAT(name) from table2 where id in ('934046', '934049');
or
select GROUP_CONCAT(name) from table2 where id in (934046,934049);但它可能仍然是这样的:
select GROUP_CONCAT(name) from table2 where id in ('934046,934049');
1.
select REPLACE(types,',','\',\'') types from table1 where id=123456
1.1
result: 934046',' 934049
1.2
select GROUP_CONCAT(name) from table2 where ids in (
select REPLACE(types,',','\',\'') types from table1 where id=123456
);
2.
select CONCAT('\'',REPLACE(types ,',','\',\''),'\'') from table1 where
id=123456
2.1
reuslt: '934046',' 934049'
2.2
select GROUP_CONCAT(name) from table2 where ids in (
select CONCAT('\'',REPLACE(types ,',','\',\''),'\'') from table1 where
id=123456); 这个字符串值不能作为参数,我应该怎么做才能以正确的方式获取in?
发布于 2018-08-03 18:49:33
select GROUP_CONCAT(t.name) from table2 t
where exists(
select 1 from table1
where id = 123456
and LOCATE(concat(', ', t.id, ',') , concat(', ', types, ',') )>0
)sql将如下所示:
select GROUP_CONCAT(t.name) from table2 t
where exists(
select 1 from table1
where id = 123456
and LOCATE(concat(',', 1, ',') , concat(', ', '1,2,3', ',') )>0
)使用函数concat()使t.id和类型具有相同的格式,然后使用LOCATE()检查类型中是否存在t.id
发布于 2018-08-03 18:06:25
$query = "select types from table1 where id = 123456";
$result = mysql_query($query) or die(mysql_error());
$array = array();
while($row = mysql_fetch_array($result))
{
$array[] = $row['types'];
}
$array = implode(",",$array);
$query2 = "select GROUP_CONCAT(name) from table2 where id in ($array)";我已经使用不同的查询创建了代码。
https://stackoverflow.com/questions/51670023
复制相似问题