Solved

MySQL query limit records in only one table on inner join

Posted on 2011-03-12
4
464 Views
Last Modified: 2012-05-11
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
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>

Open in new window

0
Comment
Question by:Lmillard
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 35116264
Here is a sample how to limit the query by one result.

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
0
 

Author Comment

by:Lmillard
ID: 35116418
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.
0
 
LVL 2

Accepted Solution

by:
cyrusjoudieh earned 500 total points
ID: 35116483
Ok, then you need to group by prodCode then
0
 

Author Closing Comment

by:Lmillard
ID: 35116559
Of course! Thanks for that, long day and cant see the wood for the trees!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql server query 12 32
IIF in access query 19 39
Search query matching words 20 38
Getting "Invalid Operation" Error when opening a Recordset 10 32
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question