[英]MySQL query, COUNT and SUM with two joined tables
I need a little help with a MySQL query. 我需要MySQL查询的帮助。
I have two tables one table is a list of backlinks
with a is_homepage
(bool) flag. 我有两个表,一个表是带有
is_homepage
(bool)标志的backlinks
的列表。 The second table is a list of the domains
for all of the backlinks
, a was link_found
(bool) flag, and a url_count
column which is the number of rows in the backlinks
table that are associated with each domain. 第二个表是所有
backlinks
的domains
列表,一个was link_found
(bool)标志和一个url_count
列,该列是backlinks
表中与每个域关联的行数。
Note that the domain_id
column is the foreign key to the domain table id
column. 请注意,
domain_id
列是域表id
列的外键。 Heres some sample data. 这里是一些样本数据。
backlinks 反向链接
id domain_id is_homepage page_href
1 1 1 http://ablog.wordpress.com/
2 1 0 http://ablog.wordpress.com/contact/
3 1 0 http://ablog.wordpress.com/archives/
4 2 1 http://www.somewhere.org/
5 2 0 http://www.somewhere.org/page=3
6 3 1 http://www.great-fun-site.com/
7 3 0 http://www.great-fun-site.com/index.html
8 4 0 http://red.blgspot.com/page=7
9 4 0 http://blue.blgspot.com/page=9
domains 域
id url_count link_found domain_name
1 3 1 wordpress.com
2 2 0 somewhere.org
3 2 1 great-fun-site.com
4 2 1 blgspot.com
The results Im looking to get from the above data would be: count = 2, total = 5 . 我希望从上述数据中得到的结果是: count = 2,total = 5 。
Im trying to get the count of rows from the domains table (count) and then the sum of the url_count
(total) from the domains table WHERE link_found
is 1 and where one of the links in the backlink table is_homepage
is 1. 我试图从域表(计数)中获取行数,然后从域表WHERE
link_found
中的url_count
(总数)之和为1,而反向链接表中的链接之一is_homepage
为1。
Here's the query I'm trying to work with. 这是我要使用的查询。
SELECT SUM(1) AS count, SUM(`url_count`) total
FROM `domains` AS domain
LEFT JOIN `backlinks` AS link ON link.domain_id = domain.id
WHERE domain.id IN (
SELECT DISTINCT(bl.domain_id)
FROM `backlinks` AS bl
WHERE bl.tablekey_id = 11
AND bl.is_homepage = 1
)
AND domain.link_found = 1
AND link.is_homepage = 1
GROUP BY `domain`.`id`
The problem with this query is that it returns a row for each entry in the domains
table. 该查询的问题在于它为
domains
表中的每个条目返回一行。 I think I might need one more sub query to add up the returned results but I'm not sure if that's correct. 我想我可能还需要一个子查询来累加返回的结果,但是我不确定这是否正确。 Does anyone see what I'm doing wrong?
有人看到我在做什么错吗? Thank you!
谢谢!
EDIT: 编辑:
The problem I'm having is that if there are more than one homepage in the back-links table then its counted multiple times. 我遇到的问题是,如果反向链接表中有多个主页,则它会被计数多次。 I need to only count each domain once.
我只需要对每个域计数一次。
Well, you shouldn't have to do a group by as you are not selecting anything other than aggregated fields. 好吧,您不必进行分组,因为除了聚合字段外,您没有选择其他任何内容。 I'm no mysql expert, but this should work:
我不是mysql专家,但这应该可以工作:
SELECT count(d.id) as count, sum(d.url_count) as total from domains as d
inner join backlinks as b
on b.domain_id = d.id
Where d.Link_found = 1 and b. is_homepage = 1
The reason you're getting a row for each entry in the domains
table is that you're grouping by domain.id
. 在
domains
表中为每个条目获得一行的原因是,您正在按domain.id
分组。 If you want grand totals only, just leave off the GROUP BY
piece. 如果您只想要总计,则不使用
GROUP BY
件。
I think a fairly simple query will do the trick: 我认为一个相当简单的查询就能解决问题:
SELECT COUNT(*), SUM(domains.URL_Count)
FROM domains
WHERE domains.link_found = 1 AND domains.id IN (
SELECT domain_id FROM backlinks WHERE is_homepage = 1)
Thanks for the help. 谢谢您的帮助。 Sorry it was so hard to explain I need a MySQL fiddle :)
抱歉,很难解释我需要MySQL提琴琴:)
If anyones interested heres what I ened up with: 如果有人对这感兴趣,我会补充一下:
SELECT SUM(1) AS count, SUM(total) AS total
FROM
(
SELECT SUM(`url_count`) total
FROM `domains` AS domain
LEFT JOIN `backlinks` AS link ON link.domain_id = domain.id
WHERE domain.id IN (
SELECT DISTINCT(bl.domain_id)
FROM `backlinks` AS bl
WHERE bl.tablekey_id = 11
AND bl.is_homepage = 1
)
AND domain.link_found = 1
AND link.is_homepage = 1
GROUP BY `domain`.`id`
) AS result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.