读书人

求两表交织的解决方案

发布时间: 2012-10-11 10:16:10 作者: rapoo

求两表交叉的解决方案
问题如下;

用户表

ID UserName
----------- --------------------------------------------------
19 无锡德利有限公司
18 江苏阳光实业有限公司

(1 行受影响)

产品表

ID ProName
----------- --------------------------------------------------
305 产品test1
307 产品test2

功能要实现给用户分配产品的数量.类似

产品test1 产品test2
无锡德利有限公司 100 3000
江苏阳光实业有限公司 105 32

因为公司可能是很多.达上千个.必定要分页.如果直接在页面上做表格填充.然后取值保存是可以的.但要分页的话是数据库输出最好. 大家有没有比较好的解决方法???








[解决办法]
楼主那些 数据是取的哪里的啊

SQL code
use tempdbif object_id('tb1') is not null drop table tb1gocreate table tb1(id int ,username nvarchar(20))goinsert into tb1select 1,'公司1'union allselect 2,'公司2'union allselect 3,'公司3'union allselect 4,'公司4'union allselect 5,'公司5'if object_id('tb2') is not null drop table tb2gocreate table tb2(id int ,proName nvarchar(20))goinsert into tb2select 1,'产品1'union allselect 2,'产品2'union allselect 3,'产品3'union allselect 4,'产品4'union allselect 5,'产品5'declare @sql nvarchar(500)set @sql=''select @sql=@sql +', 0 as '+ proName from tb2select @sql='select username '+@sql + ' from tb1 'print(@sql)exec (@sql)/***select username , 0 as 产品1, 0 as 产品2, 0 as 产品3, 0 as 产品4, 0 as 产品5 from tb1 username             产品1         产品2         产品3         产品4         产品5-------------------- ----------- ----------- ----------- ----------- -----------公司1                  0           0           0           0           0公司2                  0           0           0           0           0公司3                  0           0           0           0           0公司4                  0           0           0           0           0公司5                  0           0           0           0           0(5 行受影响)***/ 

读书人网 >SQL Server

热点推荐