如果列为空,是否有一种简单的方法可以跳过逗号?
我需要为与相同task_id匹配的所有行的逗号分隔列添加值
running
我正在使用以下内容:
============================
id | task_id | comma_sep_col
============================
1 | 20 |2,3
============================
2 | 18 |
============================
3 | 18 |1,3
============================
4 | 18 |2,3
============================
结果:
$user_id = '1';
$comma_user_id = ','.$user_id;
$sql = "UPDATE tableName set `comma_sep_col` = CONCAT(`comma_sep_col`,'$comma_user_id') WHERE find_in_set('$user_id',`comma_sep_col`) = 0 AND `task_id` = $task_id";
预期结果:
============================
id | task_id | comma_sep_col
============================
1 | 20 |2,3
============================
2 | 18 |,1
============================
3 | 18 |1,3
============================
4 | 18 |2,3,1
============================
解决:
============================
id | task_id | comma_sep_col
============================
1 | 20 |2,3
============================
2 | 18 |1
============================
3 | 18 |1,3
============================
4 | 18 |2,3,1
============================
答案 0 :(得分:3)
试试这个
$user_id = '1';
$comma_user_id = ',' . $user_id;
$sql = "UPDATE tableName
SET `comma_sep_col` = IF(
comma_user_id = '',
$user_id,
CONCAT(`comma_sep_col`, '$comma_user_id'))
WHERE find_in_set('$user_id',`comma_sep_col`) = 0
AND `task_id` = $task_id";