gpt4 book ai didi

php - 使用 PHP/MySQL 从包含多个值的列中提取单个值

转载 作者:行者123 更新时间:2023-12-01 00:54:15 24 4
gpt4 key购买 nike

我使用 implode 函数将 select 语句中的多个值插入到单个列中。现在我想单独检索这些值以供进一步查询。我有一个学生表,我在其中使用 implode 函数 implode(',', $_POST['aoi']); 将他们不同的兴趣领域存储在一个列中。表的结构是这样的:

id   |  Name   |           AOI
-------------------------------------------
101 | Paul | Hardware,Networking
102 | Fred | Finance,Insurance,Banking

现在我想根据他们的兴趣分别检索这些学生数据。我尝试使用 explode 函数提取数据。但它以数组格式返回数据。

而且我必须单独提取感兴趣的区域。谁能告诉我如何从包含多个值的列中提取单个值。这样我就可以执行进一步的查询。

喜欢$query = "select student.id,student.Name from student where AOI = 'Insurance'";

谢谢。

最佳答案

您的数据库结构从根本上是错误的,很可能会导致进一步的可伸缩性问题。你应该看看 database normalization 的概念.在这种特定情况下,您应该使用多对多关系:

学生们:

id   | name  
---------------
101 | Paul
102 | Fred

学生兴趣

id   |  student_id  | interest_id
---------------------------------
1 | 101 | 1
2 | 101 | 2
3 | 102 | 1

兴趣

id   | interest
----------------
1 | Insurance
2 | Networkings

关于php - 使用 PHP/MySQL 从包含多个值的列中提取单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564255/

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