If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Problem with Many-to-one relationship and Limits

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-10, 14:55
joshmaker joshmaker is offline
Registered User
 
Join Date: Apr 2010
Posts: 1
Exclamation Problem with Many-to-one relationship and Limits

I'm having a problem with a many-to-one relationships in a MySQL DB that I am trying to query.

I am working with three tables in a CMS (written by someone else and not easily modified). The first table lists articles, the second table lists authors and the third table maps authors to articles such that each article can have an unlimited number of authors.

When I need to select articles I can do so like this:

Code:
SELECT * FROM articles

JOIN article_authors ON article_authors.article_id = articles.id
JOIN authors ON authors.id = article_authors.author_id
This way I select one row for each article and author and get a table like this (extraneous fields removed for clarity):

Code:
ID	ARTICLE			AUTHOR
1	My Article		John
2	My Second Article	Sally
3	My Third Article	Sally
3	My Third Article	Bob
3	My Third Article	Jim
4	My Fourth Article	John
Since IDs are unique it is a simple operation to use PHP to convert these results into four different articles with one of the articles having three different authors.

Where I run into problems is on pages where I need to impose a limit on the number of articles I select. I don't know of an easy / efficient way to select the three most recent articles and all their authors.

If I impose a LIMIT on the number of rows that I select the resulting table will either truncate some of the articles or authors that I want to select.

Can someone help me find an efficient way to limit my selection to include as many rows as necessary but only three unique article ids?
Reply With Quote
  #2 (permalink)  
Old 04-14-10, 18:10
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
You can always use subqueries to limit your selection:

SELECT *
FROM (SELECT id
FROM articles
ORDER BY id DESC
LIMIT 5) a,
article_authors aa,
authors au,
articles art
WHERE art.id = a.id
AND aa.article_id = art.id
AND au.id = aa.author_id;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Tags
join help, joins, limit, many-to-one

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On