Search code examples
mysqlsqlloopsmariadbheidisql

SQL - loop query when WHERE condition comes from list


I have a mysql db table which contains a number of bibliographic records. It's formatted a little like this:

field content |field title |barcode
Black Beauty  |Title       |9781235842
James Joyce   |Author      |9781452585

There are a few dozen possible field titles.

Each record is effectively spread over a number of rows, the record is the combined rows which share the barcode.

I want to see what items have short records.

I have a working query for a specific barcode:

select barcode, sum(length(field_content)) from central where barcode = 420908032337 ;

And I have a list of 1.3k suspect barcodes. Is there a way to run the SQL query by looping through this list?

I am on a work machine and have access to HeidiSQL, git bash [including grep etc] but won't be able to install php, ruby etc for scripting.


Solution

  • 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