+ Reply to Thread
Results 1 to 10 of 10

Thread: Mysql+join+count

  1. #1
    gptsven is offline x10 Lieutenant gptsven is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    253

    Mysql+join+count

    I have this query, when I echo package_votes I get the number 5, while there actually only is one record!

    PHP Code:
    <?php  
    $sQuery 
    =  

    SELECT  
        hosts.id AS iD, 
        DATE_FORMAT(hosts.date_added,'%Y-%m-%d' ) AS added, 
        DATE_FORMAT(hosts.date_updated,'%Y-%m-%d' ) AS updated, 
        hosts.name, 
         
        package_main.freehost_id, 
        package_main.diskstorage, 
        package_main.bandwidth, 
        package_main.forcedads, 
         
        package_ratings.freehost_id, 
        uptime_ratings.freehost_id, 
        overall_ratings.freehost_id, 
        COUNT(uptime_ratings.freehost_id) AS package_votes 
         
         
         
         
         
         
         
         
         
         
         


    FROM hosts  
         
    LEFT JOIN package_main ON hosts.id = package_main.freehost_id  
    LEFT JOIN package_ratings ON hosts.id = package_ratings.freehost_id 
    LEFT JOIN uptime_ratings ON     hosts.id = uptime_ratings.freehost_id 
    LEFT JOIN overall_ratings ON hosts.id = overall_ratings.freehost_id 



    GROUP BY     
        hosts.id, 
        hosts.date_added, 
        hosts.date_updated, 
        hosts.name, 
        package_main.freehost_id, 
        package_main.diskstorage, 
        package_main.bandwidth, 
        package_main.forcedads, 
        package_ratings.freehost_id, 
        uptime_ratings.freehost_id, 
        overall_ratings.freehost_id 
         
         
    ORDER BY  

        date_added DESC LIMIT "
    .$aNavigatie['iVanaf'].", ".$iMaxPerPagina 



    ?>
    I totally do not get what is wrong :

  2. #2
    kapisco is offline x10Hosting Member kapisco is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    5

    Re: Mysql+join+count

    You see the problem lies in using the left joins. any row even it doesn satisfy the join condition will be fetched that's why u r getting a wrong count.
    so either use inner joins or post ur table structures and the result you want to get maybe i can help...

  3. #3
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Mysql+join+count

    Try using DISTINCT. I have a feeling you're getting the same data more than once.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  4. #4
    gptsven is offline x10 Lieutenant gptsven is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    253

    Re: Mysql+join+count

    well distinct didnt work...

    left join is positively the right choice here. I'm certain of one thing and that's it :P

  5. #5
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Mysql+join+count

    Count is a really tricky function, as innocuous as it may seem. I'll try to review this more thoroughly later
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  6. #6
    gptsven is offline x10 Lieutenant gptsven is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    253

    Re: Mysql+join+count

    the number 5 comes from the table package_ratings, where 5 records exist. but i thought i told sql to fetch me the number of records in uptime_ratings :/ I'm like wtf?

  7. #7
    kapisco is offline x10Hosting Member kapisco is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    5

    Re: Mysql+join+count

    gptsven
    I gave you the true solution for the problem it's the left join if you fail to see that it's up to u i've done my work here...

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Mysql+join+count

    What are the structures of the tables? I suspect you need a more restrictive join.

    Edit: I believe the DISTINCT option for SELECT operates on the final result set, after COUNT() has merged grouped rows, which is why it didn't help.

    Also, freehost_id is probably only part of the join domain you need for the ratings tables, which is why there are more rows that you expected.
    Last edited by misson; 04-28-2009 at 08:05 PM.

  9. #9
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Mysql+join+count

    Maybe this will help:

    (From mysql reference)

    COUNT(DISTINCT expr,[expr...])

    Returns a count of the number of different non-NULL values.

    COUNT(DISTINCT) returns 0 if there were no matching rows.

    mysql> SELECT COUNT(DISTINCT results) FROM student;

    In MySQL, you can obtain the number of distinct expression combinations that do not contain NULL by giving a list of expressions. In standard SQL, you would have to do a concatenation of all expressions inside COUNT(DISTINCT ...).
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  10. #10
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Mysql+join+count

    To add to what garrettroyce said: as you're joining on uptime_ratings.freehost_id, the expression COUNT(DISTINCT uptime_ratings.freehost_id) will always be 1. There should be another field/other fields you can use in a COUNT(DISTINCT ...) that should work, the same field(s) you can use to restrict the join.

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Which is better postgresql or MySQL?
    By Kurai Kumo in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 12-10-2009, 08:17 AM
  3. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  4. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers