使用多个JOINS时出错

时间:2017-02-24 09:53:24

标签: mysql sql

我正在尝试执行以下查询,但是它给出了一个语法错误,可能有些人指出我的问题是什么?

select count(*) from (((
 select * from testlink1915.TL_tcversions where execution_type = 2 and id = 66134) c 
 JOIN (select * from testlink1915.TL_nodes_heirachy) d 
   ON id = testlink1915.TL_nodes_heirachy.parent_id) a 
 JOIN (select * FROM testlink1915.TL_req_coverage where req_id = 67635) b 
   ON a.id = b.testcase_id);

只有在我添加以下细分时才会出现错误。

c JOIN (select * from testlink1915.TL_nodes_heirachy) d 
  ON id = testlink1915.TL_nodes_heirachy.parent_id

错误

  

错误代码:1064。您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'a JOIN附近使用正确的语法(select * FROM testlink1915.TL_req_coverage,其中req_id = 67635)b on a'。在第1行

3 个答案:

答案 0 :(得分:1)

使用:

c.id = d.parent_id

而不是

id = testlink1915.TL_nodes_heirachy.parent_id

试试这个:

select count(*)
from (
    select * 
    from (
        select * 
        from (
            select *
            from testlink1915.TL_tcversions
            where execution_type = 2
                and id = 66134
            ) c join (
            select *
            from testlink1915.TL_nodes_heirachy
            ) d on c.id = d.parent_id
        ) a join (
        select *
        from testlink1915.TL_req_coverage
        where req_id = 67635
        ) b on a.id = b.testcase_id
    ) t;

请注意,我到处都使用*。将其替换为您需要的列。

答案 1 :(得分:1)

请试试这个

select count(1) from
  (
  select * from (
  select * from testlink1915.TL_tcversions where execution_type = 2 and id = 66134 ) c
  JOIN
  (select * from testlink1915.TL_nodes_heirachy) d on a.id=d.parent_id
  join 
  (select * FROM testlink1915.TL_req_coverage where req_id = 67635) a  ON a.id = b.testcase_id

  ) as E join (select * from testlink1915.TL_nodes_heirachy) F  ON E.id = F.parent_id

答案 2 :(得分:0)

Try below query :

select count(*) from 
(
  (
    select * from testlink1915.TL_tcversions 
    JOIN testlink1915.TL_nodes_heirachy ON id =
    testlink1915.TL_nodes_heirachy.parent_id where execution_type = 2 and id   
    = 66134
   ) a
 JOIN
   (
     select * FROM testlink1915.TL_req_coverage where req_id = 67635
   )   ON a.id = b.testcase_id 
) ;