我在公司处理这个问题:我们有不同的客户,需要在同一个表中使用不同的字段,但我们不希望有一个包含300列的表,这是有用的,难以使用等等。例如:
table_products包含以下字段:product_id,product_name,product_cost。
然后,第一个客户端'X'需要字段product_registerid。 客户'Y'需要字段product_zipareaid。
这种情况不同。示例:它们来自不同的州,具有不同的规则。
此时我们想出了这个解决方案,我不喜欢这个: product_id,product_name,product_cost,product_personal。在此product_personal中,我们保存了诸如“{product_registerid:001; product_zipareaid:001-000131}”之类的值。
我提出了一个理论上的解决方案:扩展表,并且sql将知道我何时在扩展表中进行查询,并使用主表的列来回显列。类似的东西:
包含product_id,product_name,product_cost列的table_products。 table_products_x,列为product_registerid。 table_products_y,列为product_zipareaid。
查询将返回:
1。 select * from table_products,其中product_registerid = 001: product_id,product_name,product_cost,product_registerid 1,iphone,599,001。
2。 select * from table_products,其中product_zipareaid = 000-000110: product_id,product_name,product_cost,product_zipareaid 1,iphone,599,000-000110。
所以,我接受了解决问题的不同建议。 提前谢谢!
答案 0 :(得分:1)
一种方法是添加一个Extended Properties
表,看起来像这样:
Product_id (FK)
Client_id
PropertyName
PropertyValue
所以它会填充如下值:
Product_id Client_id PropertyName PropertyValue
1 x product_registerid 001
1 y product_zipareaid 000-000110
然后,您只需将table_products连接到Product_Id上的Extended_properties,并将所需的Client_id放在WHERE子句中。
请注意,您可能最终希望使用PIVOT查询为每个客户端获取多个扩展属性。