Search code examples
sqlmysqljoinwhere-clausesql-order-by

SQL join and where with ON (specific items) to get results in order


sample code

SELECT b.title, i.barcode
 FROM biblio b
 JOIN items i 
    ON b.biblionumber= i.biblionumber
 Join biblioitems bi 
    ON i.biblionumber=bi.biblionumber
 where i.barcode IN (4088,6183,6191)

above SQL given results as below,

enter image description here

how do I get results in IN (4088,6183,6191) as entered order


Solution

  • You can try below modified Query.

    SELECT b.title, i.barcode
    FROM biblio b
    JOIN items i ON b.biblionumber = i.biblionumber
    JOIN biblioitems bi ON i.biblionumber = bi.biblionumber
    WHERE i.barcode IN (4088, 6183, 6191)
    ORDER BY FIELD(i.barcode, 4088, 6183, 6191);
    

    Hope this helps!!