SQL 中连接和更新表的方法
所以我是这里的第一次用户,并且对 SQL 还比较陌生。我正在尝试拿两张桌子,并在某种意义上将它们连接起来。
在表 1 中,我有数据:
House_Key ---Other Fields--- Customer_ID
House_Key 不是唯一的,因此每个家庭可以有多个 Customer_ID。 House_Key 是数字代码,Customer_ID 是 Home、Business 或 Bulk。
在表 2 中,我有 House_Key 字段,但没有 Customer_ID 字段。
我的目标是创建一个新表,其中包含 Table2 的字段,其中包含一个名为 Customer_ID 的字段,但我不想像表 1 那样为每种类型的 Customer_ID 建立一个新行,而是希望每个 House_Key 只有一行,并且如果是全部三个或任意组合,Customer_ID 会显示类似“Home Business Bulk”的内容,但希望字段中始终将“Home”放在“Business”之前,然后将“Bulk”放在“Bulk”之前。
有什么办法可以做到这一点吗?提前非常感谢您。
另外,不确定这是否重要,但如果确实如此,我正在使用 SQL Server 2005。
So I am a first time user here, and still relatively new to SQL. I am attempting to take 2 tables, and join them in a sense.
In Table1 I have the data:
House_Key ---Other Fields--- Customer_ID
House_Key is not unique, and as such I can have multiple Customer_IDs per household. House_Key is a numerical code, and Customer_ID is either Home, Business, or Bulk.
In Table2 I have the House_Key field, but not the Customer_ID field.
My goal is to have a new table that holds the fields of Table2 with a field called Customer_ID, but instead of having a new row for each type of Customer_ID like in Table 1, I want to have each House_Key only have one row, and the Customer_ID say something like "Home Business Bulk" if it is all three or any combination of them, but would prefer that it always have Home before Business before Bulk in the field.
Is there any way to do this? Thank you very much ahead of time.
Also, not sure if it matters, but in case it does I'm using SQL Server 2005.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,我很高兴您来这里寻求答案,但似乎您确实需要阅读一些有关关系数据库的内容。
这里不写冗长的答案,而是链接:
http://en.wikipedia.org/wiki/Database_normalization
http://en.wikipedia.org/wiki/Join_(SQL)< /a>
Wow, I'm glad that you came here for an answer, but seems that you really need some reading about relational databases.
Instead of writing looong answer here're the links:
http://en.wikipedia.org/wiki/Database_normalization
http://en.wikipedia.org/wiki/Join_(SQL)