根据B表字段内容查询A表内容生成新表

堕落的唐僧 2014-05-05 03:48:17
比方说有1个总的视频库A表

节目名称name 视频code 栏目
aa 1234 1
bb 1235 2
cc 1236 3
dd 1237 1
ee 1238 3

如何根据点播日志的B表
视频code 点播次数 pv
1234 3
1236 3
1237 2
1238 4

去查询总表
最终希望得到一个点播详情表C
节目名称name 视频code 栏目 点播次数pv
aa 1234 1 3
cc 1236 3 3
dd 1237 1 2
ee 1238 3 4

请指教

...全文
166 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
堕落的唐僧 2014-05-05
  • 打赏
  • 举报
回复
最后再问下怎么把这一条记录 存为新的数据库表
堕落的唐僧 2014-05-05
  • 打赏
  • 举报
回复
多谢7楼的兄弟了
jiajiaren 2014-05-05
  • 打赏
  • 举报
回复
引用 5 楼 u013794332 的回复:
好像有点小问题,忘记补充1个条件了,这个code可能同时对应2栏目,这样好像会报错。 比如说 code 栏目 1234 1 1234 2

if object_id('[A]') is not null drop table [A]
go 
create table [A]([name] nvarchar(4),[code] int,[栏目] int)
insert [A]
select 'aa',1234,1 union ALL
select 'aa',1234,2 union all
select 'bb',1235,2 union all
select 'cc',1236,3 union all
select 'dd',1237,1 union all
select 'ee',1238,3
 
if object_id('[B]') is not null drop table [B]
go 
create table [B]([code] int,[pv] int)
insert [B]
select 1234,3 union all
select 1236,3 union all
select 1237,2 union all
select 1238,4

 
 
---------------------------------------------------------------查询----------------------------------------------------------------------
 SELECT c.*,d.pv FROM
(SELECT  a.name,a.code,STUFF((select  ','+CONVERT(VARCHAR,栏目)  FROM [A] b  WHERE a.code=b.code FOR XML PATH('')),1,1,'')  AS 栏目 FROM  [A] a GROUP BY
 a.name,a.code)c,[B] d WHERE c.code=d.code
 
 /*
 name code        栏目       pv
---- ----------- --------  -----------
aa   1234        1,2        3
cc   1236        3          3
dd   1237        1          2
ee   1238        3          4

(4 行受影响)

 */
發糞塗牆 2014-05-05
  • 打赏
  • 举报
回复
“这个code”是指哪个表的?整理一下你的数据
堕落的唐僧 2014-05-05
  • 打赏
  • 举报
回复
好像有点小问题,忘记补充1个条件了,这个code可能同时对应2栏目,这样好像会报错。 比如说 code 栏目 1234 1 1234 2
hmm2005 2014-05-05
  • 打赏
  • 举报
回复
select [节目名称name],A.[视频code],[栏目],[播次数 pv] from A  right join B  on A.[视频code]=B.[视频code]
山寨DBA 2014-05-05
  • 打赏
  • 举报
回复
给你个详细的:

create table A(
	name varchar(10) not null,
	code int not null,
	[栏目] int not null
 )
 create table B(
	code int not null,
	pv int not null
 )

 insert into A
 select 'aa',1234,1 union all
 select 'bb',1235,2 union all
 select 'cc',1236,3 union all
 select 'dd',1237,1 union all
 select 'ee',1238,3

 insert into B
 select 1234,3 union all
 select 1236,3 union all
 select 1237,2 union all
 select 1238,4
 
select a.name,b.code,a.[栏目],b.pv from A a inner join B b on a.code=b.code
结果: name code 栏目 pv aa 1234 1 3 cc 1236 3 3 dd 1237 1 2 ee 1238 3 4
發糞塗牆 2014-05-05
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(发粪涂墙)
-- Date    :2014-05-05 15:51:13
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
--	Apr  2 2010 15:48:46 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[A]
if object_id('[A]') is not null drop table [A]
go 
create table [A]([name] nvarchar(4),[code] int,[栏目] int)
insert [A]
select 'aa',1234,1 union all
select 'bb',1235,2 union all
select 'cc',1236,3 union all
select 'dd',1237,1 union all
select 'ee',1238,3
--> 测试数据[B]
if object_id('[B]') is not null drop table [B]
go 
create table [B]([code] int,[pv] int)
insert [B]
select 1234,3 union all
select 1236,3 union all
select 1237,2 union all
select 1238,4
--------------生成数据--------------------------



select a.* ,b.pv
from [A] INNER JOIN b ON a.code=b.code
----------------结果----------------------------
/* 
name code        栏目          pv
---- ----------- ----------- -----------
aa   1234        1           3
cc   1236        3           3
dd   1237        1           2
ee   1238        3           4
*/
山寨DBA 2014-05-05
  • 打赏
  • 举报
回复


select a.name,b.code,a.[栏目],b.pv from A a inner join B b on a.code=b.code

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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