SQL - 当 WHERE 条件来自列表时循环查询
SQL - loop query when WHERE condition comes from list
我有一个 mysql 数据库 table,其中包含许多书目记录。它的格式有点像这样:
field content |field title |barcode
Black Beauty |Title |9781235842
James Joyce |Author |9781452585
有几十个可能的字段标题。
每条记录有效分布在多行中,记录是共享条形码的组合行。
我想看看哪些项目有短记录。
我有一个特定条形码的工作查询:
select barcode, sum(length(field_content))
from central
where barcode = 420908032337
;
我有一份包含 1.3k 个可疑条形码的列表。有没有办法通过遍历此列表来 运行 SQL 查询?
我在工作机器上,可以访问 HeidiSQL、git bash [包括 grep 等],但无法安装 php , ruby 等用于编写脚本。
无需循环,只需使用 IN
运算符并在那里提供所有条形码值,例如
where barcode in (1.3K list of barcodes)
您可以通过多种方式形成该列表...使用脚本语言或任何一种。否则,创建一个 temporary
table。用那些条形码列表和临时 table
中的 select 填充它
where barcode in (select distinct barcodes from my_temp_table)
select barcode,
sum(length(field_content)) AS rec_len
from central
GROUP BY barcode
ORDER BY rec_len -- display the shortest first
LIMIT 50; -- if you want to see just 50
我有一个 mysql 数据库 table,其中包含许多书目记录。它的格式有点像这样:
field content |field title |barcode
Black Beauty |Title |9781235842
James Joyce |Author |9781452585
有几十个可能的字段标题。
每条记录有效分布在多行中,记录是共享条形码的组合行。
我想看看哪些项目有短记录。
我有一个特定条形码的工作查询:
select barcode, sum(length(field_content))
from central
where barcode = 420908032337
;
我有一份包含 1.3k 个可疑条形码的列表。有没有办法通过遍历此列表来 运行 SQL 查询?
我在工作机器上,可以访问 HeidiSQL、git bash [包括 grep 等],但无法安装 php , ruby 等用于编写脚本。
无需循环,只需使用 IN
运算符并在那里提供所有条形码值,例如
where barcode in (1.3K list of barcodes)
您可以通过多种方式形成该列表...使用脚本语言或任何一种。否则,创建一个 temporary
table。用那些条形码列表和临时 table
where barcode in (select distinct barcodes from my_temp_table)
select barcode,
sum(length(field_content)) AS rec_len
from central
GROUP BY barcode
ORDER BY rec_len -- display the shortest first
LIMIT 50; -- if you want to see just 50