简体   繁体   English

MySQL 如何对列中的每个值重复查询

[英]MySQL how to repeat query for every value in a column

This is an example of the tables and the columns used:这是使用的表和列的示例:

t_values:
╔════════════╦═══════════════╗
║ Serial     ║ Date          ║
╠════════════╬═══════════════╣
║ 1234214    ║ 2010.11.01... ║
╠════════════╬═══════════════╣
║ ABC9123213 ║ 2001.01.01... ║
╠════════════╬═══════════════╣
║ ...        ║ ...           ║
╚════════════╩═══════════════╝

t_devices:
╔════════════╦══════╗
║ Serial     ║ Type ║
╠════════════╬══════╣
║ 1234214    ║ 2    ║
╠════════════╬══════╣
║ ABC9123213 ║ 13   ║
╠════════════╬══════╣
║ ...        ║ ...  ║
╚════════════╩══════╝

I have this query to get count values grouped by weaks, but also separated by the Type field:我有这个查询来获取按弱分组的计数值,但也由Type字段分隔:

SELECT CONCAT(YEAR(V.Date), '/', WEEK(V.Date)) AS Yearweek, COUNT(*) AS QTY, D.TypeID
FROM 
t_values    AS V
JOIN 
t_devices   AS D 
ON V.Serial = D.Serial
WHERE D.TypeID = 1 --This is what I change manually
GROUP BY Yearweek
ORDER BY YEAR(V.Date) ASC, WEEK(V.Date) ASC
;

This D.TypeID column has about 13 distinct values so I need to manually change it for every value and re-run the query.这个D.TypeID列有大约 13 个不同的值,所以我需要为每个值手动更改它并重新运行查询。

But clearly this is an anti-DRY approach.但显然这是一种反 DRY 方法。 Since I'm new with MySQL I have the basic ideas about writing these kind of " static " queries but I have no particular experience or idea about the proper solutions for some more dynamic conditions.由于我是 MySQL 的新手,所以我对编写此类“ static ”查询有基本的想法,但对于一些更动态的条件的正确解决方案,我没有特别的经验或想法。

I know of the possibility of stored procedures.我知道存储过程的可能性。 Is it something that I should try to use exactly for problems like this, or is there some simpler, built-in tool to iterate this query with the different distinct values of a choosen column instead?这是我应该尝试完全用于此类问题的东西,还是有一些更简单的内置工具来使用所选列的不同不同值迭代此查询?

Edit: I was asked to clarify how would I even need the results, since I could just group by the Yearkweek column as well and leave the WHERE part.编辑:我被要求澄清我什至需要结果的原因,因为我也可以按Yearkweek列分组并留下WHERE部分。 Only that it would just give me one big table with the Yearweek-TypeID pairs.只是它只会给我一张包含Yearweek-TypeID对的大表。

I need the query I've posted with their actual own separate Type results, maybe ran dynamically after one another.我需要我发布的查询以及他们自己的实际独立类型结果,可能会一个接一个地动态运行。 And maybe I'll call this query with another language or implement it with an ORM and save the results into dataframes or excels every time on query is done.也许我会用另一种语言调用这个查询,或者用 ORM 实现它,并将结果保存到数据帧中,或者在每次查询完成时都表现出色。

So I am just not sure which direction is the preferred one.所以我只是不确定哪个方向是首选方向。 Should I use a variable and loop that with the language I run the SQL query with or create the procedure with native MySQL beforehand an call that.我应该使用一个变量并循环使用我运行 SQL 查询的语言,还是使用本机 MySQL 预先调用它创建过程。

You can get all the data in one result this way:您可以通过这种方式在一个结果中获取所有数据:

SELECT D.TypeID,
       CONCAT(YEAR(V.Date), '/',
       WEEK(V.Date)) AS Yearweek, COUNT(*) AS QTY, D.TypeID
FROM 
t_values    AS V
JOIN 
t_devices   AS D 
ON V.Serial = D.Serial
GROUP BY D.TypeID, Yearweek
ORDER BY D.TypeID,YEAR(V.Date) ASC, WEEK(V.Date) ASC
;

Or you can take input using a bound variable,或者您可以使用绑定变量获取输入,

SELECT CONCAT(YEAR(V.Date), '/', WEEK(V.Date)) AS Yearweek, COUNT(*) AS QTY, D.TypeID
FROM 
t_values    AS V
JOIN 
t_devices   AS D 
ON V.Serial = D.Serial
WHERE D.TypeID = ?
GROUP BY Yearweek
ORDER BY YEAR(V.Date) ASC, WEEK(V.Date) ASC
;

How you supply the variable is going to depend on what you are using to call SQL.您如何提供变量将取决于您使用什么来调用 SQL。

You can create a stored procedure.您可以创建一个存储过程。 The great way to call stored procedures is with a bound variable, but you could pass the argument by generating an SQL string with the argument.调用存储过程的最佳方式是使用绑定变量,但您可以通过生成带有参数的 SQL 字符串来传递参数。

CREATE PROCEDURE type_data
(IN type_in INTEGER)
BEGIN
  SELECT CONCAT(YEAR(V.Date), '/', WEEK(V.Date)) AS Yearweek, COUNT(*) AS QTY, D.TypeID
  FROM 
  t_values    AS V
  JOIN 
  t_devices   AS D 
  ON V.Serial = D.Serial
  WHERE D.TypeID = type_in
  GROUP BY Yearweek
  ORDER BY YEAR(V.Date) ASC, WEEK(V.Date) ASC
END
;

CALL type_data(1);

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

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