计数,订购和加入

时间:2016-05-15 11:16:24

标签: mysql

我的previous question给了我一个可以接受的答案

mysql> describe taps;
+------------+-----------+------+-----+-------------------+-------+
| Field      | Type      | Null | Key | Default           | Extra |
+------------+-----------+------+-----+-------------------+-------+
| tag        | int(11)   | NO   |     | NULL              |       |
| station    | int(11)   | NO   |     | NULL              |       |
| time_Stamp | timestamp | NO   |     | CURRENT_TIMESTAMP |       |
+------------+-----------+------+-----+-------------------+-------+
3 rows in set (0.00 sec)

并使用查询

SELECT tag
     , COUNT(DISTINCT station) as `visit_count` 
  FROM taps 
 GROUP 
    BY tag 
 ORDER 
    BY COUNT(DISTINCT station) DESC

按照他们访问过的电台数量来命令访客。

现在我要添加

mysql> describe visitors;
+--------+---------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| tag_id | int(11) | NO   |     | NULL    |       |
| name   | text    | NO   |     | NULL    |       |
| email  | text    | NO   |     | NULL    |       |
| phone  | text    | NO   |     | NULL    |       |
+--------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)

而且,我希望得到他的tag_idnameemail,而不是让访问者phone。我知道它涉及JOIN,但无法弄明白: - (

[更新]为了清楚起见,我想输出一个HTML表格,按访问大多数电台的人排序,显示姓名,电子邮件和电子邮件。电话

1 个答案:

答案 0 :(得分:1)

SELECT tag
     ,v.email, COUNT(DISTINCT station) as `visit_count` 
  FROM taps as t JOIN visitors as v ON t.tag = v.tag_id
 GROUP 
    BY v.email
 ORDER 
    BY COUNT(DISTINCT station) DESC