产品库
---------------------------
id cp_id cp_name
1 1001 水果
2 1002 桌子
3 1003 汽车
4 1004 手机
5 1005 药品
6 1006 电脑
7 1007 空调
用户
---------------------------
id username cp_id times
1 u5 1001
2 u1 1005
3 u5 1003
4 u2 1004
5 u3 1005
6 u4 1007
7 u5 1002
8 u5 1002
9 u8 1002
10 u5 1001 查询username 为u5 的用户..有哪些产品 如果有多个一样的产品,只显示一个.并且 用户 表中的 cp_id 只显示不重复的 u5 1001 水果
u5 1003 汽车
u5 1002 桌子
---------------------------
id cp_id cp_name
1 1001 水果
2 1002 桌子
3 1003 汽车
4 1004 手机
5 1005 药品
6 1006 电脑
7 1007 空调
用户
---------------------------
id username cp_id times
1 u5 1001
2 u1 1005
3 u5 1003
4 u2 1004
5 u3 1005
6 u4 1007
7 u5 1002
8 u5 1002
9 u8 1002
10 u5 1001 查询username 为u5 的用户..有哪些产品 如果有多个一样的产品,只显示一个.并且 用户 表中的 cp_id 只显示不重复的 u5 1001 水果
u5 1003 汽车
u5 1002 桌子
解决方案 »
- 急。。。请教高手
- c/s系统,如果出现同一条记录会同时被多个人编辑时,应该采用什么策略呀?
- 关于数据库的基本问题??
- 超级纠结的sql查询 SOS 在线等待
- 还是关于触发器的问题?
- server2003 安装SQL2000 连接不上数据库!郁闷
- 如何根据参数是否为空来查询
- 如何完整地复制数据库?
- 关于数据库转换时遇到的问题
- update cdsgus set Name=REPLACE(Name, CHAR(13) + CHAR(10), '') 速度好慢呀,有办法优化吗?
- MSDE在setup.ini中指定 instancename安装完成后,没有服务信息,找不到实例也无法启动!
- 答: 新手提问有关SQL触发器的问题 [已结贴,结贴人:vickiyan]
if object_id('[产品库]') is not null drop table [产品库]
create table [产品库] (id int,cp_id int,cp_name varchar(4))
insert into [产品库]
select 1,1001,'水果' union all
select 2,1002,'桌子' union all
select 3,1003,'汽车' union all
select 4,1004,'手机' union all
select 5,1005,'药品' union all
select 6,1006,'电脑' union all
select 7,1007,'空调'--> 测试数据: [用户]
if object_id('[用户]') is not null drop table [用户]
create table [用户] (id int,username varchar(2),cp_id int,times sql_variant)
insert into [用户]
select 1,'u5',1001,null union all
select 2,'u1',1005,null union all
select 3,'u5',1003,null union all
select 4,'u2',1004,null union all
select 5,'u3',1005,null union all
select 6,'u4',1007,null union all
select 7,'u5',1002,null union all
select 8,'u5',1002,null union all
select 9,'u8',1002,null union all
select 10,'u5',1001,nullselect distinct a.username,a.cp_id,
(select cp_name from [产品库] where cp_id=a.cp_id) as cp_name
from [用户] as a where a.username='u5'
/*
----------------
u5 1001 水果
u5 1002 桌子
u5 1003 汽车
*/
insert into #cp select 1, 1001, '水果'
union all select 2, 1002, '桌子'
union all select 3, 1003, '汽车'
union all select 4, 1004, '手机'
union all select 5 , 1005, '药品'
union all select 6, 1006, '电脑'
union all select 7, 1007 , '空调'create table #yh (id int,username varchar(5),cp_id int)
insert into #yh select 1,'u5',1001
insert into #yh select 2,'u1',1005
insert into #yh select 3,'u5',1003
insert into #yh select 4,'u2',1004
insert into #yh select 5,'u3',1005
insert into #yh select 6,'u4',1007
insert into #yh select 7,'u5',1002
insert into #yh select 8,'u5',1002
insert into #yh select 9,'u8',1002
insert into #yh select 10,'u5',1001
goselect a.username,a.cp_id,b.cp_name from (select distinct username,cp_id from #yh) a inner join #cp b on a.cp_id=b.cp_id
where a.username='u5'/*
username cp_id cp_name
-------- ----------- -------
u5 1001 水果
u5 1002 桌子
u5 1003 汽车(3 行受影响)
*/
if object_id('[产品库]') is not null drop table [产品库]
create table [产品库] (id int,cp_id int,cp_name varchar(4))
insert into [产品库]
select 1,1001,'水果' union all
select 2,1002,'桌子' union all
select 3,1003,'汽车' union all
select 4,1004,'手机' union all
select 5,1005,'药品' union all
select 6,1006,'电脑' union all
select 7,1007,'空调'--> 测试数据: [用户]
if object_id('[用户]') is not null drop table [用户]
create table [用户] (id int,username varchar(2),cp_id int,times sql_variant)
insert into [用户]
select 1,'u5',1001,null union all
select 2,'u1',1005,null union all
select 3,'u5',1003,null union all
select 4,'u2',1004,null union all
select 5,'u3',1005,null union all
select 6,'u4',1007,null union all
select 7,'u5',1002,null union all
select 8,'u5',1002,null union all
select 9,'u8',1002,null union all
select 10,'u5',1001,nullgo
select distinct a.username,a.cp_id,b.cp_name
from 用户 a
left join 产品库 b on a.cp_id = b.cp_id
where a.username = 'u5'/*
username cp_id cp_name
-------- ----------- -------
u5 1001 水果
u5 1002 桌子
u5 1003 汽车(所影响的行数为 3 行)
*/
jion pro on pro.cp_id=u.cp_id where username=5