【问题标题】:how can we add two field in same column in sql table?我们如何在sql表的同一列中添加两个字段?
【发布时间】:2014-12-27 14:52:09
【问题描述】:

我想创建一个数据库,其中包含用户购买的所有产品评分和备注

假设我创建了一个包含某些列的表名“Users_rating_product”

user_id ->INT
company->Varchar
product1 -> 3 (rating of product out of 5)|(review of product)
product2-> 4(rating)|(some review)

我想知道如何在 mysql 中做到这一点。我正在使用 phpmyadmin 创建数据库

桌子看起来像

user_id  | company  |Ac                 | TV           | fridge 

1        | goderaj  |3 ,take more power |4,less power  |5,efficient

我想知道如何做到这一点,以便产品的评分和注释都显示在同一列中

【问题讨论】:

    标签: mysql sql database phpmyadmin


    【解决方案1】:

    您可以通过多种方式实现功能。 但这不是编写查询的标准格式。

    you can have comma seperated column in your database like this
        user_id  | company  |Ac_mapping_id  
        1        | goderaj  |1,REview for Ac   
    

    对于分隔两个值,您可以这样做

    PARSENAME(REPLACE(String,',','.'),2) 'Name'
    

    对于拆分两个值的详细参考,您可以参考: click To refer

    更好的方法是将产品描述存储在不同的表中。 您可以轻松编写查询来检索数据

    user_id  | company  |Ac_mapping_id  | TV_mapping_id | fridge_mapping_id 
    1        | goderaj  |1              | 2             | 3
    

    & 像这样将所有评分和评论存储在映射表中

    Ac_mapping_id  | Rating   |Review
    1              | 1        |abcd     
    

    其他表以此类推。

    要检索所有数据,只需使用左外连接

    Select 
    *
    from
    Users_rating_product mm
    Left outer join  Ac_mapping_table ac on ac.ac_mapping_id = mm.ac_mapping_id
    .....so on
    

    【讨论】:

    • @SAC 这是数据库标准化的一种方式
    【解决方案2】:

    为此,您需要一个单独的表格。叫它ProductRating。将唯一键 (ID) 放入表中,并将该键引用到您的 Users_rating_product 表中。

    例如:

    Users_rating_product:

    user_id  | company  |Ac  | TV | fridge 
    1        | goderaj  |1   | 2  | 3
    2        | somecomp |4   | 5  | 6
    

    产品评级

    ID | Rating | Review
    1  | 3      | Take more power
    2  | 4      | less power
    3  | 5      | efficient
    4  | 5      | excellent
    5  | 1      | awful
    6  | 3      | average
    

    【讨论】:

    • 但是有很多用户,对于任何用户来说都有很多公司,那么如何关联哪个产品有哪个评论?
    • 您的表需要user_id
    • @SAC - 我在示例中添加了另一个用户/公司,向您展示它是如何工作的。
    猜你喜欢
    • 2016-03-27
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2014-10-19
    • 2019-08-09
    • 2018-05-21
    • 2021-08-30
    • 1970-01-01
    相关资源
    最近更新 更多