简体   繁体   English

拆分为逗号分隔的字符串,并在MySQL和PHP中显示

[英]Split in comma-separated string and show in MySQL and PHP

I have a small database: 我有一个小型数据库:

+-----------+-----------+------------------------+
| Name      | Number    |   Hobby                | 
+-----------+-----------+------------------------+
| Alex      | 2, 3      | Game, Shopping         |
+-----------+------------------------------------+

It's mean Number 2 is Game and Number 3 is Shopping. 这意味着2号是游戏,3号是购物。 How can I show above data like this table 我如何显示上述数据

+-----------+-----------+
| 2         | Game      |
+-----------+-----------+
| 3         | Shopping  |
+-----------+------------

Your database is not normalized. 您的数据库未规范化。 You need a third table that will be what's usually called a join table. 您需要第三个表,通常称为联接表。

The people table. The primary key is id

+-----------+-----------+
| Id        | Name      |
+-----------+-----------+
| 1         | Alex      |
| 2         | Thor      |
| 3         | Iron Man  |
| 4         | Dr Stange |
| 5         | Thanos    |
+-----------+------------

The hobbies Table

+-----------+-----------+
| Id        | Name      |
+-----------+-----------+
| 1         | Game      |
| 2         | Shopping  |
| 3         | Fighting  |
+-----------+-----------+


Join table called (for example) people_hobbies

+-----------+-----------+
| person_id | hobby_id  |
+-----------+-----------+
| 1         | 1         |
| 1         | 2         |
+-----------+-----------+

This people_hobbies table will use person_id and hobby_id to create a multi field primary key. 此people_hobbies表将使用person_id和hobby_id创建多字段主键。 This will ensure that you will not be able to add the same combination twice... which should not even make sense. 这将确保您将无法添加相同的组合两次...这甚至没有道理。

person_id is a foreign key that references the id from the people table. person_id是一个外键,它引用了人员表中的ID。 hobby_id is a foreign key that references the id from the hobbies table. hobby_id是一个外键,它引用了爱好表中的ID。

Having foreign keys will let you avoid having a key in the people_hobbies table that do not exist in both the people and the hobbies table. 具有外键将使您避免在people_hobbies表中都不存在的people_hobbies表中具有键。

The example in the table below shows that the person id 1 has two hobbies (1 and 2). 下表中的示例显示,人ID为1的人有两个爱好(1和2)。 For a human, that translates to Alex's hobbies are Game and Shopping. 对于人类而言,这意味着Alex的爱好是游戏和购物。

The above structure will let you manage your DB the way most people do. 上面的结构使您可以像大多数人一样管理数据库。

Just keep a few things in mind: 请记住以下几点:

  1. You cannot add anything in people_hobbies before they exist in both people and hobbies tables 您无法在people_hobbies中添加任何内容,然后再将其同时存在于people和hobbyies表中
  2. You must have the CASCADE UPDATE and CASCADE DELETE to the foreign key definitions so that when you delete a person or a hobby from your tables, it will remove the relationship from the people_hobbies table. 您必须对外键定义具有CASCADE UPDATE和CASCADE DELETE,以便从表中删除一个人或一个爱好时,它将从people_hobbies表中删除该关系。
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+

SELECT * FROM bad_schema;
+------+--------+----------------+
| name | number | hobby          |
+------+--------+----------------+
| Alex | 2, 3   | Game, Shopping |
+------+--------+----------------+

CREATE TABLE better_schema AS
SELECT DISTINCT name
              , SUBSTRING_INDEX(SUBSTRING_INDEX(number,',',i+1),',',-1) + 0 number
              , TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(hobby,',',i+1),',',-1)) hobby
           FROM bad_schema
              , ints;


SELECT * FROM better_schema;
+------+--------+----------+
| name | number | hobby    |
+------+--------+----------+
| Alex |      2 | Game     |
| Alex |      3 | Shopping |
+------+--------+----------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM