[英]how can we add two field in same column in sql table?
我想创建一个包含所有产品评分和用户购买说明的数据库
假设我创建一个表名“ 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
我想知道我该怎么做,以便产品的评级和注释都显示在同一列中
为此,您将需要一个单独的表。 称之为ProductRating
。 将唯一键( ID
)放在表中,然后将该键引用到Users_rating_product
表中。
例如:
Users_rating_product:
user_id | company |Ac | TV | fridge
1 | goderaj |1 | 2 | 3
2 | somecomp |4 | 5 | 6
ProductRating
ID | Rating | Review
1 | 3 | Take more power
2 | 4 | less power
3 | 5 | efficient
4 | 5 | excellent
5 | 1 | awful
6 | 3 | average
您可以通过多种方式来实现功能。 但这不是编写查询的标准格式。
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'
有关拆分两个值的详细信息,请参见: 单击以引用
更好的方法是将产品说明存储在不同的表中。 您可以轻松地编写查询来检索数据
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
对另一个表如此。
对于检索所有数据,只需使用Left外连接
Select
*
from
Users_rating_product mm
Left outer join Ac_mapping_table ac on ac.ac_mapping_id = mm.ac_mapping_id
.....so on
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.