Jump to content

SQL_CALC_FOUND_ROWS & UNION & Pagination...


JKG

Recommended Posts

hi there,

 

it would be great if you could help, i have hit a brick wall.

 

I have two queries (hence UNION), the first query is to return all the rows with a certain ID, in a certain order, this SQL works (i have removed some unnecessary bits):

 

SELECT SQL_CALC_FOUND_ROWS * FROM `users` JOIN `u_data` ON `users`.`ID`=`u_data`.`ID` WHERE `users`.`ID` in (4,5,6,7) AND `u_data`.ID NOT IN (1,2,3) ORDER BY FIELD(`u_data`.ID, 4,5,6,7)

 

the second query is to return the rest of the rows in a random, weighted order, this SQL works (i have removed some unnecessary bits):

 

SELECT * FROM `users` JOIN `u_photographers` ON `users`.`ID`=`u_photographers`.`ID` WHERE `users`.`Level_access`=2 AND `users`.`active`=1 AND `users`.`ID` NOT IN (1,2,3) ORDER BY ((`u_data`.`referral_points` + 60) * RAND(5330)) DESC

 

when i join these two queries together i am trying to use this SQL:

 

(SELECT SQL_CALC_FOUND_ROWS * FROM `users` JOIN `u_data` ON `users`.`ID`=`u_data`.`ID` WHERE `users`.`ID` in (4,5,6,7) AND `u_data`.ID NOT IN (1,2,3) ORDER BY FIELD(`u_data`.ID, 4,5,6,7)) 
UNION
(SELECT * FROM `users` JOIN `u_photographers` ON `users`.`ID`=`u_photographers`.`ID` WHERE `users`.`Level_access`=2 AND `users`.`active`=1 AND `users`.`ID` NOT IN (1,2,3) ORDER BY ((`u_data`.`referral_points` + 60) * RAND(5330)) DESC)

 

this code works fine in phpMyAdmin however this does not quite work in that when going through the pagination on the site, the first query always returns, whatever page (1,2,3,4 etc), but it should only return the selected id's, then move onto the second query...

 

any thoughts or clarification, please ask, thanks for reading. :)

Link to comment
Share on other sites

I think something like this is what you want:

 

SELECT SQL_CALC_FOUND_ROWS, * FROM
(
(
	SELECT * FROM `users` JOIN `u_data` ON `users`.`ID`=`u_data`.`ID` WHERE `users`.`ID` in (4,5,6,7) AND `u_data`.ID NOT IN (1,2,3) ORDER BY FIELD(`u_data`.ID, 4,5,6,7)
) UNION (
	SELECT * FROM `users` JOIN `u_photographers` ON `users`.`ID`=`u_photographers`.`ID` WHERE `users`.`Level_access`=2 AND `users`.`active`=1 AND `users`.`ID` NOT IN (1,2,3) ORDER BY ((`u_data`.`referral_points` + 60) * RAND(5330)) DESC
)
) as myTable LIMIT 10, 10;

Link to comment
Share on other sites

  • 2 weeks later...

Hi there again,

 

I tried to implement your suggestion but I'm getting a Duplicate column name 'ID' error #1060.

 

Should I be using a different JOIN?

 

Thanks for your continued help,

Joe.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.