在一个条件下加入两个表

时间:2017-10-18 07:19:49

标签: mysql sql vb6 firebird

我有两个表,tblstudent和tbltrans,tblstudent包含idstudent和nmstudent,在tbltrans中包含idtrans,idstudent,trprice 我想合并这两个表,以便结果变为nmstudent和trprice但我希望tbltrans中不存在的idstudent也可以显示" - "的内容。 请帮忙 Joining two table

2 个答案:

答案 0 :(得分:4)

结合左连接和合并:

select
    nmstudent,
    coalesce(trprice,'-')
from
    tblstudent
    left join tbltrans on
        tblstudent.idstudent=tblstudent.idstudent

答案 1 :(得分:1)

SELECT nmstudent, CASE WHEN tprice IS NULL THEN "-" ELSE tprice END as tprice
FROM tblstudent LEFT JOIN tblstudent
ON tblstudent.idstudent = tbltrans.idstudent