Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-08-2012, 08:16 PM   PM User | #1
topdown.me
New Coder

 
Join Date: Jul 2011
Posts: 37
Thanks: 4
Thanked 0 Times in 0 Posts
topdown.me is an unknown quantity at this point
Get the highest views from all tables

Hello
I have 2 tables and both of them has "views" column.
I want to use PHP and run a query that will show the highest views content from this two tables, mixed.

How I can do such a query?
topdown.me is offline   Reply With Quote
Old 11-08-2012, 08:46 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
SELECT MAX(U.views) FROM (
    SELECT views FROM table1
    UNION 
    SELECT views FROM table2
) AS U
You *might* get better performance from:
Code:
SELECT MAX(U.maxview) FROM (
    SELECT MAX(views) AS maxview FROM table1
    UNION 
    SELECT MAX(views) AS maxview FROM table2
) AS U
You can try it both ways.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-08-2012, 08:47 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And it has nothing to do with PHP, per se. The query will do what you want by itself and could be used standalone, with PHP, with ASP, with JSP, etc., etc.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-09-2012, 12:48 PM   PM User | #4
topdown.me
New Coder

 
Join Date: Jul 2011
Posts: 37
Thanks: 4
Thanked 0 Times in 0 Posts
topdown.me is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Code:
SELECT MAX(U.views) FROM (
    SELECT views FROM table1
    UNION 
    SELECT views FROM table2
) AS U
You *might* get better performance from:
Code:
SELECT MAX(U.maxview) FROM (
    SELECT MAX(views) AS maxview FROM table1
    UNION 
    SELECT MAX(views) AS maxview FROM table2
) AS U
You can try it both ways.
It will show only one reslt, doesnt it?
And if I want to show the the top 3?

edit:
I found out how to do it and it works,
PHP Code:
$getcontent "SELECT .. FROM
( SELECT .. FROM moviesdb
UNION ALL
SELECT  ... FROM gamesdb ORDER BY views DESC LIMIT 3) views"

but I got anothing problem.
I need to check from what databse I got the result, and I have no idea how to do it and where to place the "AS" or these things.
help?

Last edited by topdown.me; 11-09-2012 at 01:10 PM..
topdown.me is offline   Reply With Quote
Old 11-09-2012, 08:52 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
SELECT U.views, U.fromTable
FROM ( 
    SELECT views, 'table1' AS fromTable FROM table1 
    UNION 
    SELECT views, 'table2' FROM table2 
) AS U
ORDER BY views DESC
LIMIT 3
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:31 AM.


Advertisement
Log in to turn off these ads.