如何运行关于内连接的sql查询

时间:2012-12-29 16:12:50

标签: sql inner-join

select rm_id as 'ID'
        bk_branch as 'BRANCH'
        br_name as 'BANK REP.'
        bs_name as 'ESCORT'
        dt_rep  as 'DATE'
        rep_time as 'TIME'
        amt
        inv_no
        total_box
        box_no
        note_state
        dm_state
        1
        2
        10
        20
        50
        100
        500
        1000
        tp
        tv
        tp_ex1
        tv_ex1
        tp_m
        tv_m         
from bk_det 
inner join bk_rep 
  on bk_det.rm_id =  bk_rep.rm_id
inner join bk_sec 
  on bk_rep.rm_id = bk_sec.rm_id
inner join mut_det 
  on bk_sec.rm_id =  mut_det.rm_id
inner join rm_det 
  on mut_det.rm_id = rm_det.rm_id
inner join soil_det 
    on rm_det.rm_id = soil_det.rm_id

我已经从我的表中编写了上面的代码,但没有运行显示错误:

  

Msg 102,Level 15,State 1,Line 2
  'bk_branch'附近的语法不正确。

请帮助.. !!

2 个答案:

答案 0 :(得分:3)

您需要将SELECT中的字段与,分开。连接本身在语法上看起来很好。

select rm_id as 'ID'
        ,bk_branch as 'BRANCH'
        ,br_name as 'BANK REP.'
        ,bs_name as 'ESCORT'
        ,dt_rep  as 'DATE'
        ,rep_time as 'TIME'
        ,amt
        ,inv_no
        ,total_box
        ,box_no
        ,note_state
        ,dm_state
        ,1
        ,2
        ,10
        ,20
        ,50
        ,100
        ,500
        ,1000
        ,tp
        ,tv
        ,tp_ex1
        ,tv_ex1
        ,tp_m
        ,tv_m         
from bk_det 
...

答案 1 :(得分:0)

只是将表别名提供给列,否则它会给出歧义。

select a.rm_id as 'ID',
        bk_branch as 'BRANCH',
        br_name as 'BANK REP.', 
        bs_name as 'ESCORT',
        dt_rep  as 'DATE',
        rep_time as 'TIME',
        amt,
        inv_no,
        total_box,
        box_no,
        note_state,
        dm_state,
        1 ,
        2,
        10,
        20,
        50,
        100,
        500,
        1000,
        tp,
        tv,
        tp_ex1,
        tv_ex1,
        tp_m,
        tv_m,         
from bk_det a
inner join bk_rep b
  on a.rm_id =  b.rm_id
inner join bk_sec c 
  on b.rm_id = c.rm_id
inner join mut_det d
  on c.rm_id =  d.rm_id
inner join rm_det e
  on d.rm_id = e.rm_id
inner join soil_det f
    on e.rm_id = f.rm_id;