Lmillard
asked on
MySQL query limit records in only one table on inner join
Hi,
Not sure that the title really describes what I am doing here but basically as follows,
Query selecting prodHead (basic product details such as prod code and description)
Joining prodSku (multiple records per prodHead to allow for variants such as colours, size, price)
prodMenuIndex (ndex of prodHead records in the current menu section being viewed.
As the prodSku has multiple option I just want to limit the selection to 1 (the cheapest based on prodsku.net) but as you will see from the attached query it will bring back all associated prodSku records.
Any help appreciated
Cheers
Not sure that the title really describes what I am doing here but basically as follows,
Query selecting prodHead (basic product details such as prod code and description)
Joining prodSku (multiple records per prodHead to allow for variants such as colours, size, price)
prodMenuIndex (ndex of prodHead records in the current menu section being viewed.
As the prodSku has multiple option I just want to limit the selection to 1 (the cheapest based on prodsku.net) but as you will see from the attached query it will bring back all associated prodSku records.
Any help appreciated
Cheers
SELECT
prodMenuIndex.*,
prodHead.prodCode,
prodHead.prodDesc,
prodSku.prodId,
prodSku.free,
prodSku.net
FROM prodMenuIndex
INNER JOIN prodHead
ON prodMenuIndex.prodCode = prodHead.prodCode
INNER JOIN prodSku
ON prodHead.prodCode = prodSku.prodCode
WHERE prodMenuIndex.menuId = #currentMenu#
AND ProdHead.Avail = 1
AND prodSku.Avail = 1
ORDER BY #sortOrder#
<cfif IsDefined("url.StartRow") and IsDefined("ResultsPerPage")>
LIMIT #url.StartRow#, #ResultsPerPage#
</cfif>
ASKER
Hi,
The sample only brings back one record in all, the issue I have is that I want to return all records but if a prodHead record has 3 prodSku records linked I want only the cheapest prodSku record to be retrieved so if I have 3 prodhead records, 3 prodmenu records and 10 prodsku records, the query will always return 3 records as the prodsku link is limited to one but not the entire query
Sorry for not providing data samples but the tables have loads of fields so I was hoping this was an easy one that someone would just know off the top of their head.
The sample only brings back one record in all, the issue I have is that I want to return all records but if a prodHead record has 3 prodSku records linked I want only the cheapest prodSku record to be retrieved so if I have 3 prodhead records, 3 prodmenu records and 10 prodsku records, the query will always return 3 records as the prodsku link is limited to one but not the entire query
Sorry for not providing data samples but the tables have loads of fields so I was hoping this was an easy one that someone would just know off the top of their head.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Of course! Thanks for that, long day and cant see the wood for the trees!
Assuming :
prodSku Many to 1 in relation to prodHead
prodSku is Many to 1 in relation to prodMenuIndex
SELECT prodSku.prodCode,
prodHead.prodCode,
prodHead.prodDesc,
prodMenuIndex.*,
prodSku.prodId,
prodSku.free,
prodSku.net
FROM prodSku
INNER JOIN prodHead ON prodHead.prodCode = prodSku.prodCode
INNER JOIN prodMenuIndex ON prodMenuIndex.prodCode = prodHead.prodCode
WHERE prodHead.Avail = 1 AND prodSku.Avail = 1
ORDER BY prodSku.net ASC LIMIT 1
If you can provide a sample data tables it would be a lot faster.
Mysql.txt