Part of the EllisLab Network
   
 
Problem with MySQL Union and Limit
Posted: 04 July 2009 10:01 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  48
Joined  03-07-2007

Hi,

I have a quite heavy query that, a bit simplified, looks like this:

(SELECT
    ue
.id,
    
ue.type,
    
ue.title,
    
ue.body
FROM
    table1
AS t1
WHERE
    ue
.user_id = 1)
    
UNION ALL

(SELECT
    ul
.id,
    
"l" as type,
    
NULL AS title,
    
NULL AS body
FROM
    table2
AS t2
WHERE
    ue
.user_id = 1)

UNION ALL

(SELECT
    t
.id,
    
"ts" as type,
    
t.name AS title,
    
NULL AS body
FROM
    table3
AS t3
WHERE
    user_id
= 1)

ORDER BY
    timestamp DESC
LIMIT
    0
, 10

The reason I use UNION is that the tables don’t have the same columns, but I need the data to be presented together in a chronological order.

This works just fine as long as I display it in one long list, or use 0 as offset in the LIMIT. However, if i change offset to something above 0, which is needed for the page to be paginated, the page is loading forever and eventually ends up with an almost never-ending loop, with a php notice saying ‘undefined offset’ for each iteration. I guess this occurs after php reaches its timeout.

So my questions are, doesn’t UNION and LIMIT play well together? And does anyone have a clever workaround, or an alternative solution/setup for this? If anyone is willing to help out, i can provide you with more details if needed.

Thanks!
/ Patrik

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120553 Total Logged-in Users: 28
Total Topics: 126602 Total Anonymous Users: 2
Total Replies: 665577 Total Guests: 323
Total Posts: 792179    
Members ( View Memberlist )
Newest Members:  waywards88vaalionClarkKentBaoHeredamferchiivanzDr.JohnRamin.Hossainikreesmheat