I'm new to MYSQL and struggling to form a query that joins two tables and returns a unique product row.
TABLE product
:
╔══════════════════════════════╦══════════╦════════════╦════════════════════╗
║ ref ║ brand ║ mpn ║ sku ║
╠══════════════════════════════╬══════════╬════════════╬════════════════════╣
║ 0001___DOGICLON___912-101242 ║ DOGICLON ║ 912-101242 ║ 000000000001082649 ║
║ 0002___DOGICLON___912-101242 ║ DOGICLON ║ 912-101242 ║ 912-101242 ║
║ 0003___Dogiclon___912-101242 ║ Dogiclon ║ 912-101242 ║ 912-101242(R400) ║
║ 0005___Dogiclon___912-101242 ║ Dogiclon ║ 912-101242 ║ MILT-R400 ║
╚══════════════════════════════╩══════════╩════════════╩════════════════════╝
TABLE inventory
:
╔══════════════════════════════╦═══════╦═════════╦══════════╗
║ ref ║ scost ║ instock ║ location ║
╠══════════════════════════════╬═══════╬═════════╬══════════╣
║ 0001___DOGICLON___912-101242 ║ 53.68 ║ 24 ║ WA ║
║ 0001___DOGICLON___912-101242 ║ 53.68 ║ 0 ║ CA ║
║ 0002___DOGICLON___912-101242 ║ 61.00 ║ 121 ║ WA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 100 ║ WA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 0 ║ NY ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 20 ║ MA ║
║ 0003___Dogiclon___912-101242 ║ 53.53 ║ 2 ║ CA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ IN ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ MA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ WA ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 5 ║ NY ║
║ 0005___Dogiclon___912-101242 ║ 56.00 ║ 2 ║ CA ║
╚══════════════════════════════╩═══════╩═════════╩══════════╝
I guess pseduo code would be:
SHOW all products
WHERE
instock (any location) > 0 AND
(cost > 10 AND cost < 2000)
ORDER BY
cost asc
Notes:
ref
s are unique per supplierbrand
andmpn
lookup needs to be case insensitive
Expected Result:
╔══════════╦══════════╦════════════╦══════════════╦══════════════╦═══════════════════════════╗
║ ref ║ brand ║ mpn ║ sku ║ scost ║ instock ║
╠══════════╬══════════╬════════════╬══════════════╬══════════════╬═══════════════════════════╣
║ whatever ║ Dogiclon ║ 912-101242 ║ based on ref ║ based on ref ║ based on ref and location ║
╚══════════╩══════════╩════════════╩══════════════╩══════════════╩═══════════════════════════╝
This is what I'm trying:
SELECT DISTINCT
product.ref,
product.brand,
inventory.scost,
inventory.instock
FROM
product
JOIN inventory ON inventory.ref = product.ref
WHERE
inventory.instock > 1
AND ( app.inventory.scost >= 10 AND app.inventory.scost <= 2000 )
GROUP BY
product.ref