使用三张表联接查询mysql

时间:2018-08-06 07:44:14

标签: mysql group-by

我有三个表p_c_n_details,Supplier_details,pcn_type。我尝试使用这些表进行联接查询。但是分组依据有误。

我的查询:

SELECT pcn_type.name, p_c_n_details.SupplierName, COUNT(p_c_n_details.JPN_ID) 
FROM pcn_type LEFT OUTER JOIN p_c_n_details RIGHT OUTER JOIN supplier_details 
ON p_c_n_details.type = pcn_type.name AND p_c_n_details.SupplierName = 
supplier_details.SupplierName GROUP BY 
pcn_type.name,supplier_details.SupplierName;

pcn_type表:

id    |    name
-------------------------
1          Process Change
2          Design Change
3          EOL

supplier_details表:

id    |    SupplierName
------------------------
1          abc
2          def
3          ghi

p_c_n_details表:

id.   |    SupplierName    |    type           |    JPN_ID
1          abc                  Process Change      0023
2          abc                  Process Change      0024
3          abc                  Process Change      0025
4          abc                  Design Change       0026
5          abc                  Design Change       0027
6          def                  Process Change      0028
7          def                  Process Change      0029
8          def                  EOL                 0030
9          def                  EOL                 0031

预期结果:

name            |    supplier  |    total
------------------------------------------------------    
Process Change       abc            03
Design Change        abc            02
EOL                  abc             0
Process Change       def            02
Design Change        def             0
EOL                  def            02

我得到的错误:

#1064 - You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near 
'GROUP BY pcn_type.name,supplier_details.SupplierName LIMIT 0, 25' at line 1

我需要在查询中进行哪些更改以纠正错误。

3 个答案:

答案 0 :(得分:1)

您的查询中有错误,请检查以下更新的查询并与您的旧查询进行比较:

SELECT
    pcn_type. NAME,
    p_c_n_details.SupplierName,
    COUNT(p_c_n_details.id)
FROM
    pcn_type
LEFT OUTER JOIN p_c_n_details ON p_c_n_details.type = pcn_type.name 
RIGHT OUTER JOIN supplier_details ON p_c_n_details.Suppliername = supplier_details.SupplierName
GROUP BY p_c_n_details.Suppliername, p_c_n_details.type;

答案 1 :(得分:1)

您可以尝试使用

之类的查询
Select pt.name  as name,  sd.SupplierName  as supplier,count(pd.id)
from pcn_type pt 
join   supplier_details sd 
left join   p_c_n_details pd on  pd.type = pt.name and pd.Suppliername = sd.SupplierName 
group by  pt.name,  sd.SupplierName 
order by  sd.SupplierName 

输出:- enter image description here

使用表ans sql查询创建环境,您可以在其中直接测试查询。请访问小提琴以获取更多详细信息http://sqlfiddle.com/#!9/d379c3/18

答案 2 :(得分:1)

您可以尝试使用CROSS JOIN 笛卡尔积来获取pcn_type.namesupplier_details.SupplierName

您似乎想过滤名称,如果p_c_n_details表中不存在该名称,请在where中写一个子查询条件以使该名称存在于p_c_n_details中,然后使用OUTER JOINCOUNT

CREATE TABLE pcn_type(
     id int,
     name varchar(50)
);


INSERT INTO pcn_type VALUES (1,'Process Change');
INSERT INTO pcn_type VALUES (2,'Design Change');
INSERT INTO pcn_type VALUES (3,'EOL');

CREATE TABLE supplier_details(
     id int,
     SupplierName varchar(50)
);

INSERT INTO supplier_details VALUES (1,'abc');
INSERT INTO supplier_details VALUES (2,'def');
INSERT INTO supplier_details VALUES (3,'ghi');

CREATE TABLE p_c_n_details(
     id int,
     SupplierName varchar(50),
     type  varchar(50)
);


INSERT INTO p_c_n_details VALUES (1,'abc','Process Change');
INSERT INTO p_c_n_details VALUES (2,'abc','Process Change');
INSERT INTO p_c_n_details VALUES (3,'abc','Process Change');
INSERT INTO p_c_n_details VALUES (4,'abc','Design Change');
INSERT INTO p_c_n_details VALUES (5,'abc','Design Change');
INSERT INTO p_c_n_details VALUES (6,'def','Process Change');
INSERT INTO p_c_n_details VALUES (7,'def','Process Change');
INSERT INTO p_c_n_details VALUES (8,'def','EOL');
INSERT INTO p_c_n_details VALUES (9,'def','EOL');

查询1

select t.name,t.SupplierName,COUNT(t1.id) total
from
(
  SELECT p.name,s.SupplierName 
  FROM   
    pcn_type p
  CROSS JOIN 
    supplier_details s
  WHERE 
    p.name IN (SELECT DISTINCT type FROM p_c_n_details) 
  AND
    s.SupplierName IN (SELECT DISTINCT SupplierName FROM p_c_n_details) 
) t
LEFT JOIN p_c_n_details t1 on t.name = t1.type and t.SupplierName = t1.SupplierName
GROUP BY  t.name,t.SupplierName
ORDER BY  t.SupplierName

Results

|           name | SupplierName | total |
|----------------|--------------|-------|
|            EOL |          abc |     0 |
| Process Change |          abc |     3 |
|  Design Change |          abc |     2 |
| Process Change |          def |     2 |
|            EOL |          def |     2 |
|  Design Change |          def |     0 |
相关问题