简体   繁体   English

查询-第一次

[英]Querying a query - 1st time

I'm learning SQL using a textbook and a database downloaded from my PHP/SQL driven CMS website (because, like someone suggested, I have an understanding of what various query results should look like). 我正在使用从我的PHP / SQL驱动的CMS网站下载的教科书和数据库来学习SQL(因为就像有人建议的那样,我对各种查询结果应该是什么样的理解)。

I'm more familiar with MS Access where I would create queries, save them and then run queries against them. 我对MS Access更为熟悉,我可以在其中创建查询,保存查询然后针对它们运行查询。 This allowed me to break larger data analysis tasks down in to smaller, easier to comprehend pieces. 这使我可以将较大的数据分析任务分解为更小,更易于理解的部分。

Anyway, the goal of the SQL session is to find the ten people in my email database who have received the most emails. 无论如何,SQL会话的目标是在我的电子邮件数据库中查找收到最多电子邮件的十个人。 The following query provides each instance of an email being sent to an email address: 以下查询提供了发送到电子邮件地址的电子邮件的每个实例:

SELECT alert.alert_recipient,alert_sent.id
FROM alert
INNER JOIN alert_sent
ON alert_sent.alert_id=alert.id

Now, I would like to query the above query. 现在,我想查询以上查询。 If the results of the above query were a table I would want something along the lines of (I"ma newb and the syntax may be wrong): 如果上述查询的结果是一个表,我会想要一些类似的东西(I“ ma newb,语法可能是错误的):

SELECT COUNT(DISTINCT alert_recipient, ID) FROM myqueryabove
ORDER id DESC
LIMIT 10;

I asked this question in a previous post and was given an answer. 我在上一篇文章中问了这个问题,并给出了答案。 The answer gave me one query to cover it all and produce the results in one query. 答案给了我一个查询以覆盖所有内容,并在一个查询中产生结果。

What I'm asking this time around concerns the method. 我这次要问的与方法有关。 As opposed to one big query, can I save the first query as "query_1_all_emails" and then query it like a table (like I do in Access)? 与一个大查询相反,我可以将第一个查询另存为“ query_1_all_emails”,然后像表一样查询它(就像在Access中一样)吗? If I can, is this good or common practice? 如果可以的话,这是一种好习惯还是通常的做法?

You can use a view or save the result of the first query in a temporary table and then query that view / table. 您可以使用视图或将第一个查询的结果保存在临时表中,然后查询该视图/表。 However, the best method to retrieve the information you are after seems to be the use of one query only, on the line of: 但是,检索您要查找的信息的最佳方法似乎是仅使用一个查询,其行如下:

select
    count(*),
    alert_recipient
from
    alert
group by alert_recipient
order by 1 desc
limit 10;

Ways to "query a query" include: “查询查询”的方法包括:

Create a view. 创建一个视图。 Use a derived table, aka a subquery with an alias. 使用派生表,又名带有别名的子查询。 Here is how that one works: 这是一种工作方式:

select count(distinct alert_recipient, id) 
from (SELECT alert.alert_recipient,alert_sent.id
FROM alert
INNER JOIN alert_sent
ON alert_sent.alert_id=alert.id ) temp
order id desc
limit 10

Or, you can create a text file with this sql. 或者,您可以使用此sql创建文本文件。

SELECT alert.alert_recipient,alert_sent.id
FROM alert
INNER JOIN alert_sent
ON alert_sent.alert_id=alert.id
order id desc
limit 10

and copy and paste it into your query tool whenever you want to run it. 并在需要运行时将其复制并粘贴到查询工具中。 Or, some query tools will allow you to save query files so you can run them whenever you want. 或者,某些查询工具将允许您保存查询文件,以便您可以随时运行它们。

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

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