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

本文介绍了一个SQL语法错误的实例,由于使用group_concat导致查询结果中出现多余的逗号,引发了SQL语法错误。文章详细展示了错误的SQL语句及正确的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值