MySql连接多个select子查询

时间:2017-06-07 11:18:07

标签: mysql sql join subquery

- 溶液 -

SELECT Table1.unvan, Table2.sayi FROM 
(SELECT id , unvan FROM unvan WHERE id IN 
(SELECT unvani FROM personel WHERE gorev_yeri IN 
(SELECT id FROM gorev_yeri WHERE il_id="34") ) ) AS Table1 
JOIN
( SELECT unvani as id , count(*) AS sayi FROM personel WHERE gorev_yeri IN (SELECT id FROM gorev_yeri WHERE il_id="34") GROUP BY unvani ) AS Table2 
ON Table1.id = Table2.id

- 编辑 -

当我使用join时出现此错误:#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以获得在' AS table1

附近使用的正确语法

我有2个查询,我想加入他们。

It is first.

SELECT id
     , unvan 
  FROM unvan 
 WHERE id IN (SELECT unvani 
                FROM personel 
               WHERE gorev_yeri IN (SELECT id 
                                      FROM gorev_yeri 
                                     WHERE il_id="34")
              )

It is second one.

SELECT unvani as id
     , count(*) AS sayi 
 FROM personel 
WHERE gorev_yeri IN (SELECT id 
                       FROM gorev_yeri 
                        WHERE il_id="34") GROUP BY unvani

我需要这个:

unvan |卅一

-------- | ----

2 个答案:

答案 0 :(得分:1)

(SELECT id      ,unvan   来自unvan  WHERE id IN(SELECT unvani                 来自个人                在哪里gorev_yeri IN(SELECT id                                       来自gorev_yeri                                      WHERE il_id =“34”)               ) )表1

加入

( SELECT unvani as id,count(*)AS sayi FROM personel WHERE gorev_yeri IN(SELECT id FROM gorev_yeri WHERE il_id =“34”)GROUP BY unvani )表2

on table1.id = table2.id;

答案 1 :(得分:1)

基于提供的错误

应该是这样的:

SELECT `Table1`.unvan, `Table2`.id
FROM
(
    SELECT id
    , unvan 
   FROM unvan 
   WHERE id IN (SELECT unvani 
            FROM personel 
           WHERE gorev_yeri IN (SELECT id 
                                  FROM gorev_yeri 
                                 WHERE il_id="34")
          )
) AS `Table1`

JOIN

(
SELECT unvani as id
 , count(*) AS sayi 
 FROM personel 
 WHERE gorev_yeri IN (SELECT id 
                   FROM gorev_yeri 
                    WHERE il_id="34") GROUP BY unvani
) AS `Table2`

ON `Table1`.id = `Table2`.id