需要在单列中存储由逗号(,)分隔的多个值。 [英] Need to store multiple values seperated by comma(,) in single column.

查看:72
本文介绍了需要在单列中存储由逗号(,)分隔的多个值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我正在开发一个网站,其中包含n个人的个人资料。

这里用户将做的是,他/她想要登录查看人们的详细信息。只有注册用户才有权查看详细信息,他可以在一次登录时查看没有人的详细信息。

当用户点击每个个人资料时它的id应存储在数据库中。



在数据库中,我有一个名为profileid的字段。

当用户点击个人资料时第一次意味着它id存储在profileid中。



我想要的是当同一个用户点击另一个个人资料ID时,它应该与前一个用逗号(,)存储在同一个字段中。



例如:



profieid

20

20,15

20,15,10





这是我的代码:

  string 名称=( string )(会话[ 名称]); 
string time =( string )(会话[ 时间]);
string sql =( UPDATE历史记录集RegID_UserProfile =' + Request.QueryString [ id]。ToString()+ 'WHERE UserName =' + Name + '和LoginTime =' + time + ');
SqlCommand cmd7 = new SqlCommand(sql,con);
int temp = cmd7.ExecuteNonQuery();
if (temp == 1
{
// aa
}
con.Close();







同样它应该存储..



任何人都有实现这个想法???

解决方案

不,不要这样做。它违反了第一范式 [ ^ ]。每个新的profileid都应该进入自己的新行,而不是与现有值连接。您应该有2个表,比如一个用户表和另一个配置文件表(一对多关系),如下所示:

表:用户

user_id(主键)

user_name

[其他字段]



表:简介

user_id(主要用户user_id的密钥和外键表)

profile_id(主键)

[其他字段]



当用户单击配置文件时,user_id和profile_id以及其他相关信息将在配置文件表中作为新行插入一次。 user_id和profile_id的复合主键将确保没有两次插入这些值的同一对。


  SELECT   DISTINCT  [Col1],[Col2],
[Col3] = STUFF(( SELECT ' + [Col3] FROM tbl_tempTable b
WHERE b。[Col1] = a。[Col1] FOR XML PATH(' ')), 1 2 ' '
FROM tbl_tempTable a

END


Hi
I'm developing a website Which contains profile of n no of peoples.
Here what the user will do is, he/she want to log in to view the peoples details.Only register user have the rights to view the details and he can view n no peoples details on one time login.
when ever the user click on the each profile its id should store in database.

In a database i have field called profileid.
1st time when user click on the profile means its id is getting stored in profileid.

What i want is when the same user click another profile id ,then it should store in same field with previous one with comma(,).

Eg:

profieid
20
20,15
20,15,10


This is my code:

string Name = (string)(Session["Name"]);
        string time = (string)(Session["Time"]);
        string sql = ("UPDATE  History set RegID_UserProfile='" + Request.QueryString["id"].ToString() + "' WHERE UserName='" + Name + "' and LoginTime='" + time + "'");
        SqlCommand cmd7 = new SqlCommand(sql, con);
        int temp = cmd7.ExecuteNonQuery();
        if (temp == 1)
        {
            //aa
        }
        con.Close();




Likewise it should store..

Anyone have the idea to achieve this???

解决方案

No, do not do it this way. It violates the first normal form[^] of relational database design. Every new profileid should go into a new row of its own, not concatenating with existing value. You should have 2 tables, say a users table and another profiles table (one-to-many relationship) like this:
Table: users
user_id (primary key)
user_name
[other fields]

Table: profiles
user_id (primary key and foreign key to user_id of users table)
profile_id (primary key)
[other fields]

When a user clicks on a profile, the user_id and the profile_id and other related information will be inserted as a new row once in the profiles table. The composite primary key of user_id and profile_id will ensures that no same pair of these values got inserted twice.


SELECT DISTINCT [Col1], [Col2],
[Col3] = STUFF ((SELECT ', ' + [Col3] FROM tbl_tempTable b
WHERE b.[Col1] = a.[Col1] FOR XML PATH('')), 1,2,'') 
FROM tbl_tempTable a

END


这篇关于需要在单列中存储由逗号(,)分隔的多个值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆