乙個兩表關聯的複雜查詢語句

2021-06-15 22:40:26 字數 1528 閱讀 1299

表1----

id type

1  美金

2  人民幣

3  美金

表2-----

id money

1  550.00

1  300.00

1  216.00

1  210.00

2  310.00

3  410.00

結果集------

1  美金

1  550.00

1  300.00

1  美金

1  216.00

1  210.00

2  人民幣

2  310.00

3  美金

3  410.00

說明:就是把錶1每行當檔案頭,表2是細項

表2相同id每2個就寫一行檔案頭(兩個翻頁).

求此sql語句

create table #表1(id int, type varchar(10))

insert into #表1 select 1,  '美金1'

union all select 2,  '人民幣'

union all select 3,  '美金2'

create table #表2(id int, money dec(18,2))

insert into #表2 select 1,  550.00

union all select 1,  300.00

union all select 1,  216.00

union all select 1,  210.00

union all select 2,  310.00

union all select 3,  410.00

select identity(int,0,1) rowid, a.*,type into # from #表2 a

left join #表1 b

on b.id=a.id

select id,money from(

select rowid,id,cast(money as varchar(10))[money] from # union all

select min(rowid)-0.5,id,min(type) from # group by id, rowid/2)a

order by rowid

drop table #

id          money     

----------- ----------

1           美金1

1           550.00

1           300.00

1           美金1

1           216.00

1           210.00

2           人民幣

2           310.00

3           美金2

3           410.00

(所影響的行數為 10 行)

兩個MYSQL表關聯查詢

高階sql語句inner join 非常實用 在接觸這個語句之前 我要到資料庫查詢不同表的 內容我一般需要執行2次sql語句 迴圈2次。而現在有了這個語句 可以簡化成只執行1次語句 迴圈一次 result mysql query select states.state id,states.state...

兩個MYSQL表關聯查詢

高階sql語句inner join 非常實用 在接觸這個語句之前 我要到資料庫查詢不同表的 內容我一般需要執行2次sql語句 迴圈2次。而現在有了這個語句 可以簡化成只執行1次語句 迴圈一次 result mysql query select states.state id,states.state...

較為複雜的乙個linq語句

作為乙個初學者,這個句子花了我好大的力氣才寫出來,寫出來是次要的,重要的是又學到了一點新的用法。var query from gb in objectcontext.goodsclose back where gb.s sf comid gb.s month month join gw in 類似子...