解惑,在线等

hejunheng 2009-11-21 11:45:32

--select * from dbo.ProductTable where ProductCode in(
select ProductCode from dbo.CustomerProductTable where customercode=@ClientNo
--)
返回结果(其中有两条重复)
ProductCode
--------------------------------
15
13
13

这样就只有两个结果集:
其中一个为ProductCode 13的结果集,另一个为 ProductCode 15的结果集

select * from dbo.ProductTable where ProductCode in(
select ProductCode from dbo.CustomerProductTable where customercode=@ClientNo
)

也就是说,两个ProductCode的13只执行了一次
...全文
80 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hejunheng 2009-11-21
  • 打赏
  • 举报
回复
可能我没有说清楚,
其实一共3个表,
一个为用户表,其中主键为:customerNo(用户号)

用户名 用户号
---------------------
张飞 155
刘备 169

一个为产品表,其中主键为:ProductCode(产品编码)

产品名 产品编号
---------------------
青龙刀 40
赤兔马 41
方天戟 42
青罡剑 43


一个为“用户产品表”,其中两个外键:customerNo、ProductCode。分别关联用户表和产品表的主键

用户号 产品编号
---------------------
155 40
155 40
155 41
155 42
169 42
169 43


意思为编号155的用户执行完开始的语句后,只得到

-------------
青龙刀 40
赤兔马 41
方天戟 42

而不是

青龙刀 40
青龙刀 40
赤兔马 41
方天戟 42

icelovey 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pt1314917 的回复:]
你查询的两个表都不一样,一个是CustomerProductTable ,另外一个是ProductTable
结果当然不一样了。。

[/Quote]
就是,你都不是查的一个表, 是另外一个表啊..
你把2个表结构还有例子数据给出来, 这样比较好帮忙看
pt1314917 2009-11-21
  • 打赏
  • 举报
回复
你查询的两个表都不一样,一个是CustomerProductTable ,另外一个是ProductTable
结果当然不一样了。。
hejunheng 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 icelovey 的回复:]

很正常啊,你那WHERE语句等效于 ProductCode = 13 and ProductCode =15啊
没什么不对的
[/Quote]
但是我想得到的是3条结果集,有办法吗?重复的也要
icelovey 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 hejunheng 的回复:]
SQL code--select * from dbo.ProductTable where ProductCode in(select ProductCodefrom dbo.CustomerProductTablewhere customercode=@ClientNo--)返回结果(其中有两条重复)
ProductCode--------------------------------151313
这样就只有两个结果集:
其中一个为ProductCode 13的结果集,另一个为 ProductCode 15的结果集
SQL codeselect*from dbo.ProductTablewhere ProductCodein(select ProductCodefrom dbo.CustomerProductTablewhere customercode=@ClientNo
)
也就是说,两个ProductCode的13只执行了一次
[/Quote]
很正常啊,你那WHERE语句等效于 ProductCode = 13 and ProductCode =15啊
没什么不对的
hejunheng 2009-11-21
  • 打赏
  • 举报
回复
今天什么日子,我记得SQL区人很多呀
icelovey 2009-11-21
  • 打赏
  • 举报
回复
alter table [用户产品表] add AutoID int identity(1,1) not null
icelovey 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 hejunheng 的回复:]
引用 11 楼 icelovey 的回复:
引用 9 楼 hejunheng 的回复:
引用 8 楼 pt1314917 的回复:
SQL codeselect a.*from 产品表 a,用户产品表 bwhere a.产品编号=b.产品编号and b.用户号=@ClientNo

你太强了,你完全理解我的意思并解决了,
但我发现我一个致命的错。
那就是:我想对用户产品表操作的时候,却丢失唯一的用户产品表的唯一主键ID,
请问能有方法补救吗?

新加一个自增列?AUTOID

对,其实因为发现我想删除或者更新的时候却,没法操作。
[/Quote]
那你新增一个自增列就是啦, 语法好像是

alter table ztable add xID int identity(1,1) not null
hejunheng 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 icelovey 的回复:]

[/Quote]
你也非常强,而且我很佩服你
hejunheng 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 icelovey 的回复:]
引用 9 楼 hejunheng 的回复:
引用 8 楼 pt1314917 的回复:
SQL codeselect a.*from 产品表 a,用户产品表 bwhere a.产品编号=b.产品编号and b.用户号=@ClientNo

你太强了,你完全理解我的意思并解决了,
但我发现我一个致命的错。
那就是:我想对用户产品表操作的时候,却丢失唯一的用户产品表的唯一主键ID,
请问能有方法补救吗?

新加一个自增列?AUTOID
[/Quote]
对,其实因为发现我想删除或者更新的时候却,没法操作。
icelovey 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hejunheng 的回复:]
引用 8 楼 pt1314917 的回复:
SQL codeselect a.*from 产品表 a,用户产品表 bwhere a.产品编号=b.产品编号and b.用户号=@ClientNo

你太强了,你完全理解我的意思并解决了,
但我发现我一个致命的错。
那就是:我想对用户产品表操作的时候,却丢失唯一的用户产品表的唯一主键ID,
请问能有方法补救吗?
[/Quote]
新加一个自增列?AUTOID
icelovey 2009-11-21
  • 打赏
  • 举报
回复

declare @tb1 table([用户号] int,[产品编号] int)
insert @tb1
select 155,40 union all
select 155,40 union all
select 155,41 union all
select 155,42 union all
select 169,42 union all
select 169,43


declare @tb2 table([产品名] varchar(6),[产品编号] int)
insert @tb2
select '青龙刀',40 union all
select '赤兔马',41 union all
select '方天戟',42 union all
select '青罡剑',43

declare @tb3 table([用户名] varchar(4),[用户号] int)
insert @tb3
select '张飞',155 union all
select '刘备',169

SELECT A.[用户号], B.[产品编号]
FROM @tb1 A FULL JOIN @tb2 B ON A.[产品编号] = B.[产品编号]
WHERE A.[用户号] = '155'

用户号 产品编号
----------- -----------
155 40
155 40
155 41
155 42
hejunheng 2009-11-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 pt1314917 的回复:]
SQL codeselect a.*from 产品表 a,用户产品表 bwhere a.产品编号=b.产品编号and b.用户号=@ClientNo
[/Quote]
你太强了,你完全理解我的意思并解决了,
但我发现我一个致命的错。
那就是:我想对用户产品表操作的时候,却丢失唯一的用户产品表的唯一主键ID,
请问能有方法补救吗?
pt1314917 2009-11-21
  • 打赏
  • 举报
回复
select a.* from 产品表 a,用户产品表 b
where a.产品编号=b.产品编号 and b.用户号=@ClientNo
hejunheng 2009-11-21
  • 打赏
  • 举报
回复
各位,在下很急呀,心急如焚呐。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧