簡體   English   中英

我們如何在sql表的同一列中添加兩個字段?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM