Found a post very simular post to which im after. how to sort order of LEFT JOIN in SQL query? but mine is spread across a couple of tables.
Accounts
acc_id | salesRank | reg_date
1 | 50 | 2018-01-01 07:01:01
2 | 20 | 2019-02-01 07:01:01
Catalogue
cat_id | product_name | brand
1 | torch | Dewalt
2 | MacBook | Apple
3 | Phone | Samsung
Inventory
inv_id | cat_id | acc_id | salePrice | stock
1 | 1 | 1 | 999 | 6
2 | 1 | 2 | 499 | 2
Heres what I have to far:
SELECT c.cat_id, c.product_name, c.brand, a.salePrice,
MATCH(c.product_name) AGAINST('+dewalt' IN BOOLEAN MODE) AS relevancy,
(a.salesRank + i.stock) AS sellersRankTotal
FROM catalogue AS c
JOIN inventory AS i ON c.cat_id = i.cat_id
JOIN accounts AS a ON i.acc_id = a.acc_id
WHERE ... GROUP BY c.cat_id ORDER BY relevancy DESC
I some how need to get the MAX() sellersRank from 2 values across 2 tables combined.
The results that im after is the seller with the highest ranking (salesrank + stock) get the price shown in the search results.
product_name | brand | acc_id | cat_id | price | sellersRankTotal
torch | Dewalt | 1 | 1 | 999 | 56 (SalesRank + Stock)
I can only assume Amazon has a similar concept. Prices shown are not always the cheapest but sellers with a degree of ranking has their prices shown within its listings.
Hope this makes sense, Regards