Mysql 如何在其他查询中连接脚本?

Mysql 如何在其他查询中连接脚本?,mysql,mysql-workbench,Mysql,Mysql Workbench,我想将1脚本连接到另一个查询或脚本如何执行?我找不到正确的查询以下是您的原始查询: SELECT COUNT(HistoryOfVotersCollege.AuditorID) AS CountOfAuditorID, HistoryOfVotersCollege.AuditorID FROM HistoryOfVotersCollege GROUP BY HistoryOfVotersCollege.AuditorID ORDER BY COUNT(HistoryOfVotersCollege


我想将1脚本连接到另一个查询或脚本如何执行?我找不到正确的查询

以下是您的原始查询:

SELECT COUNT(HistoryOfVotersCollege.AuditorID) AS CountOfAuditorID, HistoryOfVotersCollege.AuditorID
FROM HistoryOfVotersCollege
GROUP BY HistoryOfVotersCollege.AuditorID
ORDER BY COUNT(HistoryOfVotersCollege.AuditorID) DESC; -- Query 1

SELECT AudQ1.AuditorID, AudQ1.CountOfAuditorID
FROM PresResult
WHERE AudQ1.CountOfAuditorID=(SELECT MAX(AudQ1.CountOfAuditorID) FROM AudQ1); -- Query 2
您可以使用表别名(在本例中为“h”)。这允许您声明所引用的列来自表“h”,而不是使用“HistoryOfVotersCollege”

对于下一位,您可以使用子查询创建“派生表”,这基本上是一个临时表。如果您的表很大或查询未优化,它会减慢查询速度,但它们也非常有用

SELECT der.AuditorID, der.count FROM (
SELECT h.AuditorID, COUNT(h.AuditorID) as count -- Query 1
FROM HistoryOfVotersCollege h
GROUP BY h.AuditorID) der -- this is my derived table's alias, so i can call a column by using der.AuditorID for example
join (
SELECT h.AuditorID, COUNT(h.AuditorID) as count
FROM HistoryOfVotersCollege h
GROUP BY h.AuditorID
ORDER BY count desc
LIMIT 1 -- this is the highest count for any auditor
) der2 on der.count=der2.count;
这可能会执行得很慢,但这就是问题的答案(因为这是一个连接,并使用一个查询的结果集连接到另一个查询)。 这将为您提供Voterscollege.AuditorID的所有
历史记录,其中ID的数量与最高级别的人员匹配

如果您只需要人数最多的人,您可以使用以下方法:

SELECT h.AuditorID, COUNT(h.AuditorID) as count -- Query 1
FROM HistoryOfVotersCollege h
GROUP BY h.AuditorID
ORDER BY count desc
LIMIT 1;

您可以通过以文本形式提供表和列来帮助他人。有时,当人们看到这样的问题时,他们会留下它,因为他们知道他们必须手动键入内容,如果他们可以复制+粘贴,他们会回答问题。对不起,我是新来的:/n不用担心,每个人在某个阶段都是新来的。下一次只需提供多一点上下文。您提供了正确的信息,但除此之外,只需将查询、表和列作为文本提供,以便于复制和粘贴。此外,我不倾向于在列名或表格中使用大写字母,因为这会在我的透视图中增加太多噪音,使其更难阅读,而不是更容易阅读。当然可以使用别名,这样读写查询更容易。谢谢你的提示先生:)祝你有愉快的一天我是新来的,我不能投票,我需要15个代表:/非常感谢:)
SELECT h.AuditorID, COUNT(h.AuditorID) as count -- Query 1
FROM HistoryOfVotersCollege h
GROUP BY h.AuditorID
ORDER BY count desc
LIMIT 1;