我正在尝试列出具有多个供应商的采购订单(pos)的作业的所有工作信息 以下是两个表中的列
table pos
+--------+-------+------------+
| job_id | po_id | vendor |
+--------+-------+------------+
table jobs
+--------+---------+---------+
| job_id | cust_id | jobtype |
+--------+---------+---------+
我目前不知道如何使用子查询返回列出的供应商超过1个的作业。任何帮助将不胜感激。
编辑 >>>>
mysql> select j.*, vendor, count(vendor) as count
from jobs j join pos p on p.job_id=j.job_id
group by vendor
having count(vendor) > 0;
此查询返回几乎正确的结果,除了它在表中显示两次相同的job_id,而不是重复,我只想显示不同的job_id。尝试使用distinct与job_id返回'column job_id是不明确的' 这是小组的问题吗?我需要子查询吗?
答案 0 :(得分:1)
SELECT * FROM jobs
WHERE job_id IN (
SELECT
job_id
FROM
pos
GROUP BY job_id
HAVING COUNT(DISTINCT vendor) > 1
)
答案 1 :(得分:0)
您可以使用聚合和having
子句执行此操作:
select job_id
from pos p
group by job_id
having count(distinct vendor) > 0
我明白了,你想要所有的工作信息:
select j.*
from job j
where job_id in (select p.job_id
from pos p
group by p.job_id
having count(distinct p.vendor) > 0
)
答案 2 :(得分:0)
步骤如下:
job_id
(INNER JOIN
)COUNT(DISTINCT pos.vendor)
)仅包含具有多个PO供应商(HAVING COUNT( DISTINCT pos.vendor) > 1
)
SELECT job_id, cust_id, jobtype, COUNT(DISTINCT pos.vendor)
FROM jobs
INNER JOIN pos ON jobs.job_id = pos.job_id
GROUP BY job_id, cust_id, jobtype
HAVING COUNT(DISTINCT pos.vendor) > 1
答案 3 :(得分:0)
我错过了什么。以下查询不满足哪部分内容?
SELECT j.*
FROM jobs j
JOIN pos p
ON p.job_id = j.job_id
GROUP
BY j.job_id
HAVING COUNT(*) > 1;