【mysql】[error]group_concat造成的sql语法错误

521 查看

sql错误实例

$sql_id = "select group_concat(`table_a_id`) from `table_b`";
$id_str = $db->execute($sql_id);

$sql = "select * from `table_a` where `id` in ( {$id_str} ) and `is_effect` = 1`";
$res = $db->execute($sql);

/*  
    error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right 
    syntax to use near ') and `is_effect` = 1'
*/
// 完整sql: select * from `table_a` where `id` in ( 1,2,3,4, ) and `is_effect` = 1`

错误原因

sql中多了一个逗号
group_concat 执行结构有大小限制,会根据byte大小进行截取

参考:mysql手册-group_concat