我有三个表(其中两个实际上是“视图”)。我想离开联接表2和3,并按它们的两个公用列对其进行分组,然后进行一些计数。我整夜都在挣扎,我正要把笔记本电脑丢到窗外
这是我的三个桌子:
+-----------------------+
| Features |
+-----------------------+
| featureId | title |
+-----------+-----------+
| feature1 | Feature 1 |
+-----------+-----------+
| feature2 | Feature 2 |
+-----------+-----------+
| feature3 | Feature 2 |
+-----------+-----------+
+--------------------------+--------+
| Tasks | |
+--------------------------+--------+
| featureId | workstreamId | title |
+-----------+--------------+--------+
| feature1 | workstream1 | Task 1 |
+-----------+--------------+--------+
| feature2 | workstream2 | Task 2 |
+-----------+--------------+--------+
| feature2 | workstream3 | Task 3 |
+-----------+--------------+--------+
| feature2 | workstream3 | Task 3 |
+-----------+--------------+--------+
+--------------------------+-------+
| Jobs | |
+--------------------------+-------+
| featureId | workstreamId | title |
+-----------+--------------+-------+
| feature3 | workstream1 | Job 1 |
+-----------+--------------+-------+
| feature2 | workstream2 | Job 2 |
+-----------+--------------+-------+
| feature2 | workstream3 | Job 3 |
+-----------+--------------+-------+
这是我想要得到的结果:
+-----------+--------------+-----------+----------+
| featureId | workstreamId | taskCount | jobCount |
+-----------+--------------+-----------+----------+
| feature1 | workstream1 | 1 | 0 |
+-----------+--------------+-----------+----------+
| feature2 | workstream2 | 1 | 1 |
+-----------+--------------+-----------+----------+
| feature2 | workstream3 | 2 | 1 |
+-----------+--------------+-----------+----------+
| feature3 | workstream1 | 0 | 1 |
+-----------+--------------+-----------+----------+
我想按featureId和workstreamId分组-此组合在结果中应该是唯一的。我觉得这应该真的很容易,但我只是想不通。
这是我到目前为止所拥有的:
SELECT
f.title,
t.workstreamId,
t.taskCount,
j.jobCount
FROM features f
LEFT JOIN (
SELECT
featureId,
workstreamId,
COUNT(workstreamId) AS taskCount
FROM tasks
GROUP BY workstreamId, featureId
) AS t
ON t.featureId = f.featureId
LEFT JOIN
(
SELECT
featureId,
workstreamId,
COUNT(workstreamId) AS jobCount
FROM jobs
GROUP BY featureId, workstreamId
) AS j
ON j.featureId = f.featureId
欢迎任何帮助:-)
答案 0 :(得分:1)
最简单的方法是将features
连接到tasks
和jobs
表的UNION上,并对不同的标题进行COUNTS。诀窍在于,在UNION中,NULL
表中的任务标题为tasks
,而NULL
表中的任务标题为jobs
,因此允许正确添加计数对于每种类型:
SELECT f.featureId, u.workstreamId, COUNT(u.tasktitle) AS taskCount, COUNT(u.jobtitle) AS jobCOunt
FROM features f
JOIN (SELECT featureId, workstreamId, 'Task' AS tasktitle, NULL AS jobtitle
FROM tasks
UNION ALL
SELECT featureId, workstreamId, NULL AS tasktitle, 'Job' AS jobtitle
FROM jobs) u
ON u.featureId = f.featureId
GROUP BY f.featureId, u.workstreamId
ORDER BY f.featureId
输出:
featureId workstreamId taskCount jobCOunt
feature1 workstream1 1 0
feature2 workstream2 1 1
feature2 workstream3 2 0
feature3 workstream1 0 1
答案 1 :(得分:0)
您需要按function get_price() {
$url = "https://steamcommunity.com/market/priceoverview/?country=PL¤cy=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case";
$json = file_get_contents($url);
$price = json_decode($json);
$price_case = $price->{"lowest_price"};
return number_format($price_case, 2);
}
和Tasks
表创建完整表。
下一步在完整表的基础上为Jobs
和LEFT JOIN
编写两个Tasks
子查询,然后获取Jobs
count
答案 2 :(得分:0)
FULL JOIN
首先查询子查询,然后LEFT JOIN
将结果查询到features
。
SELECT f.featureid,
coalesce(t.workstreamid, j.workstreamid) workstreamid,
coalesce(t.count, 0) taskcount,
coalesce(j.count, 0) jobcount
FROM features f
LEFT JOIN ((SELECT featureid,
workstreamid,
count(*) count
FROM tasks
GROUP BY featureid,
workstreamid) t
FULL JOIN (SELECT featureid,
workstreamid,
count(*) count
FROM jobs
GROUP BY featureid,
workstreamid) j
ON t.featureid = j.featureid
AND t.workstreamid = j.workstreamid)
ON coalesce(t.featureid, j.featureid) = f.featureid;
当标题可为空并且包含空值时,它也可以工作:db<>fiddle