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 8271131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:46:02+00:00 2026-06-08T06:46:02+00:00

Hi i know basic MySQL query format, but now i have a need to

  • 0

Hi i know basic MySQL query format, but now i have a need to do some complex query.
say (for explanation purpose)
i have 3 tables with column id, item_name, and no_of_views.
i want to a single query to get top 10 viewed items from these 3 table.

Table1

 id:        item_name        no_of_views
 1          item1             20
 2          item2             25
 3          item3             16
 4          item4             10

Table2

 id:        item_name        no_of_views
 1          itemA             2
 2          itemB             5
 3          itemC             70
 4          itemD             0

Table3

 id:        item_name        no_of_views
 1          item_1             34
 2          item_2             55
 3          item_3             10
 4          item_4             1

i know i have to join these tables and then query based on no_of views

like

 $query="
 (SELECT * FROM Table2)
 UNION ALL
 (SELECT * FROM Table2)
 UNION ALL
 (SELECT * FROM Table2)
 ORDER by no_of _views DESC LIMIT 10";

This works perfectly fine but my problem is my tables have 100’s of item in them and i have many tables, if this is the way i do it the query will have to get all the results from all the tables and then give me only top 10 views,

i want to do it like selecting only 10 values from all tables based on no of views
like say i have 10 tables and each one have a no_of_views, how can i query to see which item has higest views among all tables and then next lower no_of_views from another table or same table which ever is true and so on.
is that even possible.

please suggest.

  • 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-08T06:46:03+00:00Added an answer on June 8, 2026 at 6:46 am

    When comparing your query to the following query, the MySQL Execution plans are identical. The optimizer will still only be selecting the top 10 from each table (not the full record set) and then comparing them as that is the maximum it will need. This method is not as inefficient as you seem to believe.

    $query="
    (SELECT * FROM Table1 ORDER BY no_of_views DESC LIMIT 10)
    UNION ALL
    (SELECT * FROM Table2 ORDER BY no_of_views DESC LIMIT 10)
    UNION ALL
    (SELECT * FROM Table3 ORDER BY no_of_views DESC LIMIT 10)
    ORDER by no_of _views DESC LIMIT 10";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know this is basic but somehow i have been stuck here for some
I do know some basic differences but there are still some questions in my
I need to know some basic information on the for command in batch scripts.
What I have is a mySQL query that will select some drinks and return
I have a very basic MySQL Database. This is the query I used to
I'm not expert about css menus. But I know basic system to make css
Although I know the basic concepts of binary representation, I have never really written
I know about the basic concept of virtual function and run-time call. But i
I am relatively new to grails and need to know something really basic. Right
I guess this query is a little basic and I should know more about

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.