首先,我是MySQL的初学者,我仍在学习。我正在尝试加入2个表格以显示计数。首先,我使用2个代码。一种显示名称的代码-
SELECT tag_logs.timestamp, People.Name FROM `tag_logs` INNER JOIN People WHERE tag_logs.tag_no = People.nametag
另一个显示姓名计数的代码-
SELECT tag_logs.tag_no, COUNT(tag_logs.tag_no) FROM tag_logs GROUP BY tag_no HAVING COUNT(tag_no) >= 1
我要显示名称和计数编号,而不是标签编号和计数。我尝试通过使用以下代码来连接两个表,但是,我运气不佳-
SELECT People.Name FROM `tag_logs` INNER JOIN People WHERE tag_logs.tag_no = People.nametag AND COUNT(tag_logs.tag_no) FROM tag_logs GROUP BY tag_no HAVING COUNT(tag_no) >= 1
当我第二次尝试调用“ FROM tag_logs”时,出现错误。有办法解决这个问题吗?
I aim to make this my final result, except I should be able to see names instead of numbers.
答案 0 :(得分:0)
应该是
SELECT People.Name FROM `tag_logs`
INNER JOIN People on tag_logs.tag_no = People.nametag
GROUP BY tag_no HAVING COUNT(tag_no) >= 1
编辑
SELECT People.Name, COUNT(tag_no) FROM `tag_logs`
INNER JOIN People on tag_logs.tag_no = People.nametag
GROUP BY tag_no HAVING COUNT(tag_no) >= 1
答案 1 :(得分:0)
使用(reduce + numbers) ;; Compute the sum of all the numbers
子句连接两个表。您应该学习加入。
ON
答案 2 :(得分:0)
我相信您想要的查询如下:
SELECT p.Name, COUNT(*)
FROM tag_logs tl INNER JOIN
People p
ON tl.tag_no = p.nametag
GROUP BY p.Name;
注意:
COUNT(*)
比COUNT(tl.tag_no)
短,并且它们执行相同的操作。GROUP BY
子句现在与SELECT
匹配。如果可以有同名的人,则将p.nametag
添加到GROUP BY
。仅使用GROUP BY tl.tag_no
的版本是无效的SQL,并且由于p.Name
中不匹配的SELECT
,在大多数数据库中都会失败。HAVING
子句(HAVING COUNT(tag_no) >= 1
)是不必要的,因为INNER JOIN
至少需要一个匹配项,并且tag_no
永远不会NULL
(因为使用了它) JOIN
)。