具有多个连接的 MySQL 子查询

时间:2021-03-18 06:15:50

标签: mysql

嗨,我是 MySQL 的新手,我的 SQL 查询有问题。

问题

我想要做的是,通过 JOIN 从多个表中获取所有数据,然后从返回的结果中使用“project_manager_id”和父查询中的搜索值过滤掉数据。

不确定我的东西是否按正确顺序排列。

我想在一个查询中实现这一点。

/* FILTER OUT THE DATA BASED ON THE ID AND SEARCH VALUE */
SELECT j.project_manager_id 
FROM  tritech_reporting.job j
WHERE j.project_manager_id IN
(
/* SELECT ALL THE DATA */
SELECT j.id AS job_id, j.project_manager_id, j.request, j.total, j.est_labor_hours, j.est_gross_profit, j.act_labor_hours, j.remaining, c.id AS customerID, c.company_name, si.name AS site_name, cc.type
FROM tritech_reporting.job j 
INNER JOIN tritech_reporting.site si ON j.site_id = si.id 
INNER JOIN tritech_reporting.customer c ON j.customer_id = c.id
INNER JOIN tritech_reporting.cost_center cc ON j.id = cc.job_id 
WHERE cc.type = 'Product' OR cc.type = 'Monitoring' OR cc.type = 'Projects Direct' OR cc.type = 'Projects Indirect'
GROUP BY job_id
)
AND j.project_manager_id = 7
AND c.company_name = '%spot%'
ORDER BY job_id DESC;

当前退货

Error Code: 1241. Operand should contain 1 column(s)

结果

结果是从大量结果中过滤数据。

目前内部Sql查询返回数据,简化如下:

job_id | project_manager_id | request   | total
5001   | 380                | Tech test | 3,454.56
5003   | 7                  | Spotless  | 3,454.56
5031   | 380                | Tech test | 3,454.56
5041   | 7                  | Spot Free | 3,454.56
5033   | 380                | Tech test | 3,454.56

外部过滤器运行后:

job_id | project_manager_id | request   | total
5001   | 7                  | Spotless  | 3,454.56
5001   | 7                  | Spot Free | 3,454.56

2 个答案:

答案 0 :(得分:0)

您的嵌套选择应该只选择一列:

/* FILTER OUT THE DATA BASED ON THE ID AND SEARCH VALUE */
SELECT j.project_manager_id 
FROM  tritech_reporting.job j
WHERE j.project_manager_id IN
(
/* SELECT ALL THE DATA */
SELECT j.project_manager_id FROM tritech_reporting.job j 
INNER JOIN tritech_reporting.site si ON j.site_id = si.id 
INNER JOIN tritech_reporting.customer c ON j.customer_id = c.id
INNER JOIN tritech_reporting.cost_center cc ON j.id = cc.job_id 
WHERE cc.type = 'Product' OR cc.type = 'Monitoring' OR cc.type = 'Projects Direct' OR cc.type = 'Projects Indirect'
GROUP BY job_id
)
AND j.project_manager_id = 7
AND c.company_name = '%spot%'
ORDER BY job_id DESC;

因为你只比较了一列

答案 1 :(得分:0)

我设法找出问题所在,我将查询返回到前面。 我更改了内部查询以获取用户和类型,并更改外部查询以获取其他所有内容。

这是最终的工作查询。

/* FILTER OUT THE DATA BASED ON THE ID AND SEARCH VALUE */
SELECT j.id AS job_id, j.project_manager_id, j.request, j.total, 
 j.est_labor_hours, j.est_gross_profit, j.act_labor_hours, j.remaining, 
 c.id AS customerID, c.company_name, si.name AS site_name, cc.type 
FROM  tritech_reporting.job j
INNER JOIN tritech_reporting.site si ON j.site_id = si.id 
INNER JOIN tritech_reporting.customer c ON j.customer_id = c.id
INNER JOIN tritech_reporting.cost_center cc ON j.id = cc.job_id
WHERE j.project_manager_id IN
(
/* SELECT ALL THE DATA */
SELECT j.project_manager_id FROM tritech_reporting.job j 
WHERE cc.type = 'Product' OR cc.type = 'Monitoring' 
OR cc.type = 'Projects Direct' OR cc.type = 'Projects Indirect'
)
AND j.project_manager_id = 7
AND c.company_name LIKE '%spot%'
GROUP BY job_id
ORDER BY job_id DESC;
相关问题