Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7880535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:10:03+00:00 2026-06-03T04:10:03+00:00

I have a script that makes a union with different tables of my database.

  • 0

I have a script that makes a union with different tables of my database.
I need to show the result with pagination to make it more readable to the user.

This is my code:

//Fetch
foreach($query_info->result() as $info){

//I'm going to create a query for each row i get from the db
$query [] = (' (
    SELECT 
        '.$info->slug.'_dog.`name`, 
        `age`,
        '.$info->slug.'_breed.`name` as breed, 
        `sex`, 
        `link`,
        `sterilized`, 
        \''.$info->slug.'\' as prefix 
    FROM 
        '.$info->slug.'_dog, 
        '.$info->slug.'_breed 
    WHERE 
        '.$info->slug.'_dog.breed = '.$info->slug.'_breed.id 
        AND 
        '.$info->slug.'_dog.type = '.$type.'
)';
}

//implode different UNION
$query_final = implode(" UNION ",$query);

As you can see the the query could be very big.
How can I paginate?

My second option is make a full View with all the result of the tables, about 10-20 tables…
Which is faster for the database? the “big” UNION or a View?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:10:05+00:00Added an answer on June 3, 2026 at 4:10 am

    I don’t know which is faster, but to do the UNION, you can add an offset and limit to the end of the total query for your pagination. In only the first query, you can add SQL_CALC_FOUND_ROWS in the select. Something like this: (Note that there is no comma after SQL_CALC_FOUND_ROWS)

    (SELECT SQL_CALC_FOUND_ROWS column1, column2 FROM etc…) UNION (SELECT column1, column2 FROM etc…) LIMIT 0,10

    After you have ran the query, immediately run this query:

    SELECT FOUND_ROWS();

    It will return the total records like there was no LIMIT on your query. So basically you do not have to run the query again to count the results.

    For creating the rest of the pagination, you can use CI it’s own pagination library:
    http://codeigniter.com/user_guide/libraries/pagination.html

    // Edit:
    For returning data from the model, you could use something like this:

    $results[‘data’] = $this->db->query($sql)->result_array();

    $total = $this->db->query(‘SELECT FOUND_ROWS() as total’)->row_array();

    $results[‘total’] = $total[‘total’];

    return $results;

    It can probably be optimized a bit more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With my database upgrade scripts, I typically just have one long script that makes
I have a python script that makes about ten INSERTs into a MySQL database.
I have this basic script that makes an element show onmouseenter, and hide onmouseleave.
I have a bash script that performs makes and then aborts when a make
I have a script that makes use of a package (PKG_MY_PACKAGE). I will change
I have to write a bash script that makes lot of things. I'd like
I have a python script that uses threads and makes lots of HTTP requests.
I have a python function that makes a subprocess call to a shell script
I have a script that uses BeautifulSoup that I want to make into a
For an assignment in college, we have to make a script in Perl that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.