SQL 连接 4 个表未返回正确计数
SQL join on 4 tables not returning correct count
我有 4 个 table。
序列
id -> primary key
name
user_id -> foreign key (users id)
人:
id-> primary key
name-> varchar
seq_id -> foreign key from sequences
user_id -> foreign key from users
links_clicked:
id-> primary key
src_id -> foreign key (sequences id)
views -> Boolean
people_sequence
sequence_id-> foreign key (sequences id)
people_id -> foreign key (people id)
我需要获取序列详细信息,link 的点击次数 link table,如果 seq_id 在那里的人数以及人数序列来自 people_sequence table 的序列。
我想我需要使用左连接,我编写了如下查询:
SELECT sequences.*
,sum(link_clicked.view) as click_rate
,count(people.seq_id) as prospect
,count(people_sequences.sequence_id) as seqret
FROM sequences
LEFT JOIN link_clicked ON sequences.id=link_clicked.src_id
LEFT JOIN people ON sequences.id=people.seq_id
LEFT JOIN people_sequences ON sequences.id=people_sequences.sequence_id
where sequences.user_id = 1
group by sequences.id
但这是返回每个计数列的计数相同
无法弄清楚 SQL
有什么问题
id | name | user_id |click_rate | prospect|seqret
5ac77be52d06e | seq1 |1 | 414 | 1890 | 1890
5ae790b48fea9 | seq2 |1 | 8 | 43 | 0
5ae790b6e0b12 | seq3 |1 | 2 | 0 | 0
5ae835c5153b5 | seq4 |1 | NULL | 0 | 0
这是我得到的结果,但如果我这样做 SQL 观看次数总和 different.and 计数也不同
SELECT `src_id` ,sum(`view`) FROM `link_clicked ` group by src_id
src_id | sum(`view`)
5ac77be52d06e | 23
5ae790b48fea9 | 8
5ae790b6e0b12 |2
我认为那些左连接会使您的结果变得疯狂...您将获得所有内容的多个重复条目,因为连接的所有排列都是作为导致无意义的结果生成的 sums/counts。要查看所有重复结果,请删除聚合,您将得到一个非常长的重复和误导性列表。试试 3 个子查询?
SELECT S.*
,(SELECT COUNT(IIF([view], 1, 0)) FROM link_clicked WHERE src_id=S.id) click_rate
,(SELECT COUNT(*) FROM people WHERE seq_id=S.id) prospect
,(SELECT COUNT(*) FROM people_sequences WHERE sequence_id=S.id) seqret
FROM sequences S
WHERE S.user_id=1;
我有 4 个 table。
序列
id -> primary key
name
user_id -> foreign key (users id)
人:
id-> primary key
name-> varchar
seq_id -> foreign key from sequences
user_id -> foreign key from users
links_clicked:
id-> primary key
src_id -> foreign key (sequences id)
views -> Boolean
people_sequence
sequence_id-> foreign key (sequences id)
people_id -> foreign key (people id)
我需要获取序列详细信息,link 的点击次数 link table,如果 seq_id 在那里的人数以及人数序列来自 people_sequence table 的序列。 我想我需要使用左连接,我编写了如下查询:
SELECT sequences.*
,sum(link_clicked.view) as click_rate
,count(people.seq_id) as prospect
,count(people_sequences.sequence_id) as seqret
FROM sequences
LEFT JOIN link_clicked ON sequences.id=link_clicked.src_id
LEFT JOIN people ON sequences.id=people.seq_id
LEFT JOIN people_sequences ON sequences.id=people_sequences.sequence_id
where sequences.user_id = 1
group by sequences.id
但这是返回每个计数列的计数相同
无法弄清楚 SQL
有什么问题id | name | user_id |click_rate | prospect|seqret
5ac77be52d06e | seq1 |1 | 414 | 1890 | 1890
5ae790b48fea9 | seq2 |1 | 8 | 43 | 0
5ae790b6e0b12 | seq3 |1 | 2 | 0 | 0
5ae835c5153b5 | seq4 |1 | NULL | 0 | 0
这是我得到的结果,但如果我这样做 SQL 观看次数总和 different.and 计数也不同
SELECT `src_id` ,sum(`view`) FROM `link_clicked ` group by src_id
src_id | sum(`view`)
5ac77be52d06e | 23
5ae790b48fea9 | 8
5ae790b6e0b12 |2
我认为那些左连接会使您的结果变得疯狂...您将获得所有内容的多个重复条目,因为连接的所有排列都是作为导致无意义的结果生成的 sums/counts。要查看所有重复结果,请删除聚合,您将得到一个非常长的重复和误导性列表。试试 3 个子查询?
SELECT S.*
,(SELECT COUNT(IIF([view], 1, 0)) FROM link_clicked WHERE src_id=S.id) click_rate
,(SELECT COUNT(*) FROM people WHERE seq_id=S.id) prospect
,(SELECT COUNT(*) FROM people_sequences WHERE sequence_id=S.id) seqret
FROM sequences S
WHERE S.user_id=1;