Cross Table Queries

Discussion in 'MySQL' started by Seph, Oct 24, 2006.

  1. #1
    Say I have 10 tables each containing a field `hits`. If I want to find the top 10 records ordered by hits in descending order, can I archieve this with 1 query or will I have to use a query for each table and then sort the resulting 10 arrays?
     
    Seph, Oct 24, 2006 IP
  2. rkcca

    rkcca Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could do this in a stored procedure.

    (Psuedo code)

    Create TmpTable

    insert into TmpTable (select top 10 hits from Table1 order by hits desc)

    repeat for all desired tables.

    select top 10 hits from TmpTable order by hits desc

    Drop TmpTable

    Temp tables are not the "best" way to go, but this will save you trips across the wire and array manipulation.
     
    rkcca, Oct 24, 2006 IP
  3. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #3
    i am sure you can achieved by one query
     
    php-lover, Oct 24, 2006 IP
  4. sdlifecycle

    sdlifecycle Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Have you considered a UNION clause? That should get you what you need.

    select top 10 hits as 'HitCount' from tblOne
    order by dtOne desc
    union
    select top 10 hits from tblTwo
    order by dtTwo desc
     
    sdlifecycle, Oct 24, 2006 IP
  5. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #5
    You probably need to change your database structure. I can't imagine a script that requires something like that.
     
    SoKickIt, Oct 25, 2006 IP
    Forum-Angels likes this.
  6. rosytoes

    rosytoes Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Agreed, the only fields that should be present in many tables are primary/foreign keys.
     
    rosytoes, Oct 25, 2006 IP
  7. Seph

    Seph Peon

    Messages:
    84
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well about the structure, Had I put everything in one table it would have had a lot of records I and would have needed several extra fields describing which part of the website the record belong to. So I decided to make it different tables.

    The top record orderd by hits is not a nessesary part but a welcome one and changing the structure now is too much work.

    And I'll look into the UNION clause.
     
    Seph, Oct 25, 2006 IP
  8. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #8
    SoKickIt, Oct 25, 2006 IP
  9. Seph

    Seph Peon

    Messages:
    84
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah not gonna happen, this is a personal site which no one is going to take over and it fits all my needs besides this one which isn't even nessesary.

    I do know it's a better way of doing databases, but these were done a long time ago when I knew hardly anything about database structure, and honestly I can't be bothered to change something that works, despite it being a better solution, appreciate the advice though.
     
    Seph, Oct 25, 2006 IP
  10. yoogi

    yoogi Peon

    Messages:
    56
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    if you have designed it right, there should be only one table which has all diffrent values of hits once and only once.

    Other tables may have hits filed, but they will either have repettion or incomplete for various reasons. (one hit belonging to several category will result in same hit being repeated for all categories).

    SO querying the table where HITS is unique, is enough to list hits.

    But if you want to display more information along with hits, (say price or disk format of record) then you should add all the tables that have those information to the query.

    If you are making a list of hits in one page, the usual prcatice will be to show only a snap shot of information and use seperate page for showing all details.
    (ie, like showing hit-title, Author/singer, Released-by, price) in the list-page and complete detail with description for a single hit/record in a separate page.

    May be you can show the table names and filed names here so that people can help better.
     
    yoogi, Oct 25, 2006 IP