gpt4 book ai didi

php - SQL如何从多行的逗号分隔列表中删除一个数字

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:52 25 4
gpt4 key购买 nike

假设我的数据库中有一个表,如下所示:

|-------------------------|
| id | numbers |
|-------------------------|
| 1 | 1,3,5,7 |
| 2 | 2,4,6,8 |
| 3 | 1,2,3,4,5,6,7,6,8 |
|-------------------------|

我想为数字列中具有数字 4 的所有行删除让我们说 4。对此的sql调用是什么?我正在使用 php 和 mysql。

最佳答案

为了使其更易于理解,我使用了不同的表名和列名。更好的表格设计是

users table
-------------------
id
name
other_columns


roles table
-------------
user_id
role_number

示例数据:

users
--------------
id name
1 peter
2 tom


roles
----------
user_id role_number
1 1
1 3
1 7
2 2
2 8

使用这种设计,您现在可以像这样查询用户拥有的所有角色

select r.role_number
from users u
join roles r on u.id = r.user_id
where u.name = 'peter'

或者如果您已经有了用户 ID

select role_number
from roles
where user_id = 1

关于php - SQL如何从多行的逗号分隔列表中删除一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25641925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com