我的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_id
,name
和email
,而不是让访问者phone
。我知道它涉及JOIN
,但无法弄明白: - (
[更新]为了清楚起见,我想输出一个HTML表格,按访问大多数电台的人排序,显示姓名,电子邮件和电子邮件。电话
答案 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