1

表格1

                HolId
1   Package17   2,1,5
2   Package13   5,4,3

表 2

 HolId1
    1   New Years Day   2013-01-01 00:00:00.000
    2   Republic Day    2013-01-26 00:00:00.000
    3   Holi            2013-04-27 00:00:00.000
    4   Memorial Day    2013-05-27 00:00:00.000
    5   US Independence 2013-07-04 00:00:00.000
    7   Labour Day  2013-09-02 00:00:00.000

我想显示如下数据

         HolId    HolId1
Package17   2   2   Republic Day    2013-01-01 00:00:00.000
Package17   1   1   New Years Day   2013-04-27 00:00:00.000
Package17   5   5   US Independence 2013-05-27 00:00:00.000
Package13   5   5   US Independence 2013-07-04 00:00:00.000
Package13   4   4   Memorial Day    2013-05-27 00:00:00.000
Package13   3   3   Holi            2013-04-27 00:00:00.000

我的网格将显示如下,我已经弄清楚了

Package17

     Republic Day   2013-01-01 00:00:00.000
    New Years Day   2013-04-27 00:00:00.000
    US Independence 2013-05-27 00:00:00.000

Package13   

    US Independence 2013-07-04 00:00:00.000
    Memorial Day    2013-05-27 00:00:00.000
    Holi            2013-04-27 00:00:00.000
4

2 回答 2

4

这是解决方案MySQL

SELECT
T1.Pkg, T2.*
FROM Table2 T2
    JOIN Table1 T1
        ON FIND_IN_SET(T2.HolID1, T1.HolID)

SQL 小提琴演示

这是为了SQL SERVER

SELECT
T1.Pkg, T2.*
FROM Table2 T2
    JOIN Table1 T1
        ON CHARINDEX(CAST(T2.HolID1 as varchar), T1.HolID) > 0

SQL 小提琴演示

于 2013-03-16T18:24:30.617 回答
0

最好对您的表进行反规范化,然后只需一次连接即可获得结果。如果您必须使用当前架构。希望以下代码对您有所帮助。


declare @tempTable table 
     ( Package_Name varchar(100),
       Holid int
       )
declare @SQLString varchar(max) declare @package_name varchar(100) set @SQLString = '' DECLARE @X XML while @SQLString is not null begin set @SQLString = null select top 1 @package_name=package_name, @SQLString = Holid from Package where package_name not in (select package_name from @tempTable) if @SQLString is null break SET @X = CAST('' + REPLACE(@SQLString, ',', '') + '' AS XML) insert into @tempTable SELECT @package_name, t.value('.', 'INT') FROM @x.nodes('/A') as x(t) end select * from @tempTable select A., B.* from @tempTable A inner join HolID1 B on A.Holid = B.HolID1

于 2013-03-16T19:12:54.757 回答