简体   繁体   English

将“其他”数据存储到 MySQL 数据库中的最佳方式

[英]Best way to store "Other" data into MySQL database

I have a form with radio buttons which stores the value (which is the ID) in my MySQL database along with the necessary information from the user.我有一个带有单选按钮的表单,它在我的 MySQL 数据库中存储值(即 ID)以及来自用户的必要信息。

INSERT INTO table (user_id, name, address, prefer_id) VALUES ('', ?, ?, ?);

So when I try to fetch the data, I use LEFT JOIN , to get the necessary description from table2 :因此,当我尝试获取数据时,我使用LEFT JOINtable2获取必要的描述:

SELECT a.name, a.address, b.prefer_desc FROM table a
LEFT JOIN table2 b ON a.prefer_id = b.prefer_id
WHERE a.user_id = ?

But I have created an other option, in case the option the user prefers is not in the list.但是我创建了另一个选项,以防用户喜欢的选项不在列表中。 A textbox will appear when the user selects Other in the list of radio buttons so they can type-in freely their preferred data.当用户在单选按钮列表中选择Other时,将出现一个文本框,以便他们可以自由输入他们喜欢的数据。 Check this fiddle to see an example.检查此小提琴以查看示例。

The first logic that I've thinked of is to create a separate table which stores the typed-in data of the user.我想到的第一个逻辑是创建一个单独的表来存储用户输入的数据。

other_tb:其他_tb:

 other_id | user_id | typed_in |
----------+---------+----------+
    1     |     1   |   cake   |
    2     |     3   |   pizza  |

So when I fetch the data, I use php 's if() condition if the prefer_id is 4 (or other), and if it does, I will use another SELECT query to get the other data in other_tb table.因此,当我获取数据时,如果prefer_id4 (或其他),我将使用phpif()条件,如果是,我将使用另一个SELECT查询来获取other_tb表中的其他数据。

SELECT typed_in FROM other_tb WHERE user_id = ?

Is there a way to do all of this in a single query?有没有办法在一个查询中完成所有这些?

OR或者

Is this the best option, or is there a right or better way in this kind of situation?这是最好的选择,还是在这种情况下有正确或更好的方法?

try this尝试这个

SELECT 
    a.name, 
    a.address, 
    IF(a.prefer_id=4,(SELECT typed_in FROM other_tb WHERE user_id = a.user_id),b.prefer_desc) as prefer_desc
FROM table a
LEFT JOIN table2 b 
    ON a.prefer_id = b.prefer_id
WHERE a.user_id = ?

If you are using the second table as you described, and you want to keep the same format of the output and assuming the records in other_tb are unique for each user_id , you could use something like this:如果您按照您的描述使用第二个表,并且您希望保持相同的输出格式并假设other_tb中的记录对于每个user_id都是唯一的,您可以使用以下内容:

SELECT a.name, a.address, 
       CASE 
           WHERE a.prefer_id = 4 THEN c.typed_in
           ELSE b.prefer_desc
       END CASE AS prefer_desc
FROM table a
LEFT JOIN table2   b ON a.prefer_id = b.prefer_id
LEFT JOIN other_tb c ON a.user_id   = c.user_id
WHERE a.user_id = ?

I think this can also be written as:我觉得这也可以写成:

SELECT a.name, a.address, 
       CASE a.prefer_id
           WHERE 4 THEN c.typed_in
           ELSE b.prefer_desc
       END CASE AS prefer_desc
FROM table a
LEFT JOIN table2   b ON a.prefer_id = b.prefer_id
LEFT JOIN other_tb c ON a.user_id   = c.user_id
WHERE a.user_id = ?

There are two approaches that I was able to practice depending on the situation:根据情况,我可以练习两种方法:

  1. Allow users to add more options so that their answer is still indexed to table2 .允许用户添加更多选项,以便他们的答案仍被索引到table2 So when fetching data, you may still use LEFT JOIN to get the user's preferred option without any other condition.所以在获取数据时,你仍然可以使用LEFT JOIN来获取用户的首选选项,而无需任何其他条件。 But this method will allow users to see the options that was added by other users.但是这种方法将允许用户查看其他用户添加的选项。
  2. If the options are "fixed" and only the administrator is allowed to add options to table2 , have another column to table1 , which stores the other answer of the user (let say other_option column).如果选项是“固定的”并且只有管理员被允许向table2添加选项,则在table1有另一列,它存储用户的other答案(比如other_option列)。 This method allows the user to still freely state their option without adding options to table2 .此方法允许用户仍然自由地声明他们的选项,而无需向table2添加选项。

Here is table1 :这是表table1

+---------+------+---------+-----------+--------------+
| user_id | name | address | prefer_id | other_option |
+---------+------+---------+-----------+--------------+

What I do is I put 0 to prefer_id if the user prefers to answer other_option .我做的是我把0到prefer_id如果用户更喜欢回答other_option So your program expects that if the prefer_id is 0, you have an input in other_option .所以,你的程序要求,如果prefer_id是0,你必须在输入other_option

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

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