Alex Buckley
Feb 2024
(If you don't have a test site to use)
Koha reports wiki
Koha Database Schema
Important
SELECT table.column FROM table
SELECT count(*) FROM table
SELECT table.column1, table.column2, table.column3 FROM table
SELECT table.* FROM table
SELECT biblio.biblionumber FROM biblio
SELECT count(*) FROM items
SELECT borrowers.firstname, borrowers.surname, borrowers.email FROM borrowers
SELECT count(*) FROM table WHERE table.column = "value"
SELECT table.column1, table.column 2 FROM table WHERE table.column IS NULL
SELECT table.column1 FROM table WHERE table.column != "value"
SELECT count(*) FROM statistics WHERE statistics.type = "issue" LIMIT 10;
SELECT count(*) FROM items WHERE items.barcode IS NULL
SELECT count(*) FROM reserves WHERE reserves.itemnumber IS NOT NULL
SELECT table.column AS "Column name" FROM table SELECT table.column AS ColumnName FROM table
SELECT borrowers.firstname AS "First name" FROM borrowers SELECT borrowers.surname AS Surname FROM borrowers
SELECT table.column FROM table WHERE table.column LIKE "%middle%";
SELECT table.column FROM table WHERE table.column NOT LIKE "%value%";
SELECT table.column FROM table WHERE table.column LIKE "%end";
SELECT table.column FROM table WHERE table.column LIKE "start%";
SELECT borrowers.surname AS Surname, borrowers.firstname AS "First Name", borrowers.cardnumber AS "Card Number", borrowers.email AS Email FROM borrowers WHERE borrowers.email NOT LIKE '%@%.%'
SELECT biblio.title FROM biblio WHERE biblio.title LIKE "%dog%";
SELECT DISTINCT(table.column) FROM table
SELECT DISTINCT(items.itype) FROM items
SELECT table.column1, table.column2 FROM table WHERE table.column = 'data' AND table.column != 'data' AND table.column >= 'number'
SELECT items.itemnumber, items.barcode FROM items WHERE items.homebranch = 'CPL' AND items.ccode = 'FIC'
SELECT items.* FROM items WHERE items.ccode = 'FIC' AND items.dateaccessioned > '2021';
SELECT table.column1, table.column2 FROM table WHERE table.column = "data" OR table.column IS NOT NULL
SELECT items.biblionumber, items.itemnumber FROM items WHERE items.itemlost = 1 OR items.damaged = 1 OR items.withdrawn = 1 OR items.notforloan = 1
SELECT table.column FROM table WHERE table.column = <<Label for text input>>
SELECT table.column FROM table WHERE table.column = <<Label for dropdown|dropdown>>
SELECT biblio.biblionumber, biblio.title FROM biblio WHERE biblio.frameworkcode = <<Framework code|biblio_framework>>
SELECT items.itemnumber, items.barcode FROM items WHERE items.ccode = <<Collection|CCODE>>
SELECT table.column FROM table WHERE table.column = "data" ORDER BY table.column ASC
SELECT table.column FROM table WHERE table.column = "data" ORDER BY table.column DESC
SELECT borrowers.surname, borrowers.firstname FROM borrowers WHERE borrowers.branchcode = "CPL" ORDER BY borrowers.surname ASC
SELECT borrowers.surname, borrowers.firstname FROM borrowers WHERE borrowers.branchcode = "CPL" ORDER BY borrowers.firstname DESC
SELECT table.column FROM table LIMIT num_of_rows
SELECT biblio.* FROM biblio WHERE biblio.datecreated > '2020' LIMIT 5;
SELECT biblio.* FROM biblio WHERE biblio.datecreated > '2020' ORDER BY biblio.title ASC LIMIT 5;
SELECT COUNT(table.column), table.column FROM table GROUP BY table.column
SELECT SUM(table.column), table.column FROM table GROUP BY table.column
SELECT COUNT(items.itemcallnumber) AS 'Number of Items', items.itemcallnumber FROM items GROUP BY items.itemcallnumber
HAVING - filter results after grouping
SELECT COUNT(table.column1), table.column2 FROM table GROUP BY table.column2 HAVING COUNT(table.column1) > 'num'
SELECT GROUP_CONCAT(biblioitems.biblionumber SEPARATOR ', ') AS 'Duplicate biblionumbers', biblioitems.isbn FROM biblioitems GROUP BY biblioitems.isbn HAVING COUNT(biblioitems.isbn) > 5
Return all biblionumbers where there are more than 5 duplicates
SELECT ExtractValue(table.metadata,'//datafield[@tag="tag"]/*'), ExtractValue(table.metadata,'//datafield[@tag="tag"]/subfield[@code="subfield"]'), ExtractValue(table.metadata,'//datafield[@tag="tag"]/subfield[@code>="subfield"]') FROM table
Table will always be biblio_metadata or auth_header
SELECT ExtractValue(biblio_metadata.metadata, '//datafield[@tag="245"]/*') AS 'Full Title information' FROM biblio_metadata WHERE biblio_metadata.biblionumber=14
SELECT ExtractValue(biblio_metadata.metadata, '//datafield[@tag="245"]/subfield[@code="a"]') AS 'Title' FROM biblio_metadata WHERE biblio_metadata.biblionumber=14
Creating SQL reports that use more than one Koha module
SELECT a.column, b.column FROM table a JOIN table b USING (sameColumnName) WHERE a.column = "value"
SELECT a.column, b.column FROM table a JOIN table b ON (a.column = b.column) WHERE c.column = "value"
SELECT b.biblionumber, b.title, i.barcode FROM biblio b JOIN items i USING (biblionumber) WHERE b.frameworkcode=<<Framework code>>
SELECT DISTINCT(b.title), i.itype, bi.itemtype FROM items i JOIN biblio b USING (biblionumber) JOIN biblioitems bi ON (i.itype=bi.itemtype)
Three SQL queries are run:
SELECT
DISTINCT borrowers.branchcode
FROM borrowers
WHERE borrowers.branchcode IS NOT NULL
ORDER BY borrowers.branchcode
Fetch distinct (unique) values for rows
SELECT DISTINCT borrowers.categorycode
FROM borrowers
WHERE borrowers.categorycode IS NOT NULL
ORDER BY borrowers.categorycode
Fetch distinct values for columns
SELECT borrowers.branchcode, borrowers.categorycode, COUNT(*)
FROM borrowers
WHERE borrowers.dateofbirth > '1990-01-01'
AND borrowers.dateofbirth < '2023-01-31'
GROUP BY borrowers.branchcode, borrowers.categorycode
Count by row/column combinations - include filters in a WHERE clause
Three SQL queries are run:
SELECT DISTINCTROW items.homebranch
FROM items
LEFT JOIN biblioitems USING (biblioitemnumber)
LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
WHERE 1
ORDER BY items.homebranch
SELECT DISTINCTROW items.itype
FROM items
LEFT JOIN biblioitems
USING (biblioitemnumber)
LEFT JOIN biblio
ON (biblioitems.biblionumber = biblio.biblionumber)
WHERE 1 AND items.itype LIKE 'BK'
ORDER BY items.itype
SELECT items.homebranch, items.itype, COUNT(*)
FROM items
LEFT JOIN biblioitems ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
WHERE 1 AND items.itype LIKE 'BK'
GROUP BY items.homebranch, items.itype
ORDER BY items.homebranch,items.itype
SELECT items.homebranch, items.itype, COUNT(DISTINCT biblioitems.biblionumber)
FROM items
LEFT JOIN biblioitems ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
WHERE 1 AND items.itype LIKE ?
GROUP BY items.homebranch, items.itype
ORDER BY items.homebranch, items.itype
Try these out:
SELECT items.biblionumber, items.itemnumber, items.itype, items.homebranch, items.holdingbranch FROM items WHERE items.itype IS NULL OR items.itype = '' OR items.homebranch IS NULL OR items.homebranch = '' OR items.holdingbranch IS NULL OR items.holdingbranch = '';
SELECT items.itemnumber, items.dateaccessioned FROM items WHERE items.dateaccessioned BETWEEN DATE_SUB( CURDATE(), INTERVAL 7 DAY ) AND CURDATE();
SELECT biblio.biblionumber, biblio.datecreated FROM biblio WHERE biblio.datecreated BETWEEN DATE_SUB( CURDATE(), INTERVAL 7 DAY ) AND CURDATE();
SELECT items.itemnumber, items.datelastseen, items.itemlost_on FROM items WHERE items.itemlost IS NOT NULL;
SELECT * FROM branchtransfers WHERE branchtransfers.datesent IS NOT NULL AND branchtransfers.datearrived IS NULL AND branchtransfers.datecancelled IS NULL;
SELECT t.itemnumber, frb.branchname, tob.branchname FROM branchtransfers t LEFT JOIN branches frb ON (t.frombranch = frb.branchcode) LEFT JOIN branches tob ON (t.tobranch = tob.branchcode) WHERE t.datesent IS NOT NULL AND t.datearrived IS NULL AND t.datecancelled IS NULL;