2左连接指向1个表

时间:2013-06-19 17:45:51

标签: sql

SELECT
*
FROM dvcam

LEFT JOIN format AS format1 ON
dvcam.ID_Format=format.ID_Format,

originaltape
LEFT JOIN format AS format2 ON
originaltape.RO_Format=format.ID_Format

任何人都可以帮我吗?

格式包含ID_Format和格式保存名称(dvd,vhs等)

我需要从dvcam表和originaltape

中显示格式(名称)

5 个答案:

答案 0 :(得分:0)

快速猜测:

SELECT
*
FROM dvcam
LEFT JOIN format AS format1 ON
dvcam.ID_Format=format1.ID_Format,

originaltape
LEFT JOIN format AS format2 ON
originaltape.RO_Format=format2.ID_Format

答案 1 :(得分:0)

您需要在第一次加入后删除,并修复on子句中的别名:

SELECT *
FROM dvcam left join
     format AS format1
     ON dvcam.ID_Format=format1.ID_Format left join
     originaltape left join
     format AS format2
     ON originaltape.RO_Format=format2.ID_Format

答案 2 :(得分:0)

您需要解释更多以及您使用的数据库(Oracle,SQL Server,MYSql等)

修改 你需要定义dvcam和originaltape之间的关系,如果与originaltape的dvcam关系不是1-1,那么你所拥有的连接将无法工作。另一方面,如果它是1-1那么你可以做这样的事情

Select *
from
  (SELECT *
    FROM dvcam inner join 
      format as f1 
     /*assuming every record has a format field */
     on dvcam.id_format =f1.id_format) dv inner join
  (SELECT *
    FROM originaltape inner join 
      format as f2 
     /*assuming every record has a format field */
     on originaltape.id_format =f2.id_format) ot 
  on dv.?? = ot.??

HTH 贾法

答案 3 :(得分:0)

你可以尝试这个,它应该适用于任何数据库:

SELECT
*
FROM dvcam
LEFT JOIN format AS format1 ON
dvcam.ID_Format=format1.ID_Format
LEFT JOIN originaltape ON
originaltape.RO_Format=format1.ID_Format

答案 4 :(得分:-3)

这就是我所做的..现在它可以工作..

SELECT dvcam.*, format.Format AS Format, Format2.Format AS Format2

FROM
dvcam
LEFT JOIN format ON
dvcam.ID_Format=format.ID_Format,

originaltape
LEFT JOIN format AS format2 ON
originaltape.RO_Format=format2.ID_Format

感谢您的所有帮助和时间..