简体   繁体   English

只需数一数

[英]Simply count one-to-many

I am trying to count the number of hits per country in my database, which has the following tables: 我试图计算数据库中每个国家/地区的点击数,该数据库具有以下表格:

mysql> use DB; show tables;
+--------------+
| Tables_in_DB |
+--------------+
| Hits         |
| Tags         |
| HitTags      |
+--------------+

And here are some example columns from those tables: 这是这些表中的一些示例列:

mysql> select * from Hits;
+---------+----------+--------------+
| HitID   | HitDate  | HitText      |     
+---------+----------+--------------+
| 0123456 | 01/01/12 | access page1 |
| 7890123 | 01/02/12 | access page2 |
| 4567890 | 01/03/12 | access page3 |
+---------+----------+--------------+

mysql> select * from Tags;
+-----------+---------+---------+
| TagID     | TagType | TagText |     
+-----------+---------+---------+
| 123123213 | country | USA     |
| 456456456 | country | Brazil  |
| 789789789 | country | China   |
| 012345678 | city    | London  |
+-----------+---------+---------+

mysql> select * from HitTags;
+---------+-----------+
| HitID   | TagID     |
+---------+-----------+
| 0123456 | 123123213 |
| 7890123 | 456456456 |
| 4567890 | 789789789 |
+---------+-----------+

I want to fetch the number of hits per country, such as in the following SQL query {and pseudo code}: 我想获取每个国家/地区的点击数,例如以下SQL查询{和伪代码}:

SELECT DISTINCT TagText, TagID FROM Tags
    WHERE TagType = 'country';

for each $row {
    SELECT COUNT(HitID) As HitCount FROM HitTags
        WHERE HitTags.TagID = $row[Tags.TagID];
}

I have no issue doing this when using separate queries, but I am sure there is a way of doing this more elegantly (ie using a single SQL query instead of one-per-country). 使用单独的查询时,我没有问题,但是我敢肯定,有一种方法可以更优雅地做到这一点(即使用单个SQL查询而不是每个国家/地区)。

Maybe using a JOIN, maybe using the ANY keyword. 也许使用JOIN,也许使用ANY关键字。 Or maybe using some nested SELECT statements? 还是使用一些嵌套的SELECT语句?

Here is what I have tried: 这是我尝试过的:

SELECT COUNT(HitID) AS HitCount, TagText FROM HitTags
    JOIN Tags ON Tags.TagID =
    ANY (SELECT TagID FROM Tags WHERE TagType = 'country');

But that returns a single row with the total of all hits (from everywhere, although a country is listed along with the result). 但这会返回一行总命中数的单行(来自世界各地,尽管列出了一个国家和结果)。

Ideally, the result would be something like: 理想情况下,结果应为:

+---------+----------+
| TagText | HitCount |
+---------+----------+
|     USA |     1234 |
|  Brazil |     5678 |
|   China |     9012 |
+---------+----------+

I know this must be quite common (as in "yeah, I saw this in MySQL class 101")... 我知道这一定很常见(例如,“是的,我在MySQL类101中看到了”)...

I searched the web a lot, but everybody seems to be using a single table (instead of three). 我在网上搜索了很多,但每个人似乎都在使用一个表(而不是三个表)。 I'm really no good with those "one-to-many" relationships. 这些“一对多”的关系对我真的不好。 Sorry to be such a newbie! 抱歉成为这样的新手!

Any idea, hint or advice would be useful, thanks in advance! 任何想法,提示或建议都会有用,在此先感谢您!

You are just missing a GROUP BY clause: 您只是缺少GROUP BY子句:

SELECT 
  COUNT(HitID) AS HitCount,
  TagText 
FROM HitTags
  JOIN Tags ON Tags.TagID =
    ANY (SELECT TagID FROM Tags WHERE TagType = 'country')
GROUP BY TagText;

Actually, this is better with a LEFT JOIN : 实际上,使用LEFT JOIN会更好:

SELECT
  COUNT(HitID) AS HitCount,
  TagText
FROM 
  Tags 
  LEFT JOIN HitTags ON Tags.TagID = HitTags.TagID
GROUP BY TagText

Try this: 尝试这个:

SELECT *, (SELECT COUNT(HitID) FROM HitTags WHERE TagID = t.TagID ) AS hit_count 
FROM Tags t WHERE TagType = 'country'

This would do the same but I think is faster. 可以这样做,但是我认为速度更快。

Try the following 尝试以下

SELECT COUNT(HitID) AS HitCount, TagText FROM HitTags
JOIN Tags ON Tags.TagID =
ANY (SELECT TagID FROM Tags WHERE TagType = 'country')
group by TagText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM