建视图

qingqingyang 2009-02-05 10:54:17
表ShopShare,结构如下
id(int) ShopID(varchar) OldSkeeper
1 13423,13422,13421,13420 15
2 13418,13417,13416,13415 15
3 13347,13276,13275,13274 18

表shop,结构如下
Sid (int) ShopName(varchar) ShopKeeper
11111 EEEEEE 15
13423 AAAAAA 15
13422 CCCCCC 15

要建一个视图,结构如下(想要得到的数据是shop表中sid 在ShopShare表shopid存在的记录)
ShopID(int) ShopName(varchar) ShopKeeper
13423 AAAAAA 15
13422 CCCCCC 15

...全文
60 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihan6415151528 2009-02-05
  • 打赏
  • 举报
回复
charindex函数
第一个参数你要查找的char,第二个参数你被查找的字符串 返回参数一在参数二的位置
select charindex('a','lihan')
-----------
4
水族杰纶 2009-02-05
  • 打赏
  • 举报
回复
--視圖
create view view_name
as
select b.Sid as ShopID , b.ShopName , b.ShopKeeper from shop b inner join shopshare a on charindex(','+ltrim(b.sid)+',',','+ltrim(a.shopid)+',')>0
rucypli 2009-02-05
  • 打赏
  • 举报
回复
create view View_XX
as
select B.sid,B.shopName,B.shopKeeper
from shopshare A,shop B
where charindex(B.sid,A.shopID)>0
水族杰纶 2009-02-05
  • 打赏
  • 举报
回复
--表ShopShare,结构如下 
if object_id('shopshare')is not null drop table shopshare
go
create table shopshare(id int, ShopID varchar(30), OldSkeeper int)
insert shopshare select 1, '13423,13422,13421,13420', 15
insert shopshare select 2, '13418,13417,13416,13415' , 15
insert shopshare select 3, '13347,13276,13275,13274' , 18
if object_id('shop')is not null drop table shop
go
create table shop(Sid int , ShopName varchar(20), ShopKeeper int)
insert shop select 11111, 'EEEEEE' , 15
insert shop select 13423, 'AAAAAA', 15
insert shop select 13422, 'CCCCCC' , 15
select b.* from shop b inner join shopshare a on charindex(','+ltrim(b.sid)+',',','+ltrim(a.shopid)+',')>0
/*Sid ShopName ShopKeeper
----------- -------------------- -----------
13423 AAAAAA 15
13422 CCCCCC 15*/
水族杰纶 2009-02-05
  • 打赏
  • 举报
回复
select b.*  from shop b inner join shopshare a on charindex(','+ltrim(b.sid)+',',','+ltrim(a.shopid)+',')>0

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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