Pull information from 2 databases and order it?

Discussion in 'MySQL' started by HsHosting, Mar 25, 2009.

  1. #1
    I am really new to MySQL and am Trying to teach myself :p But im stuck on this

    Im not sure how to explain what I am trying to do here, but Here is what I have so far

    using code
    <?
    include"mysql.php";
    $getoffers=mysql_query("SELECT COUNT(*) `user_id` FROM `completed` GROUP BY `user_id` ORDER BY `user_id` DESC",$c);
    while($off=mysql_fetch_array($getoffers))
    {
    print"        
                    <li>User - {$off['user_id']}</li>    
        
    ";
    }
    ?>
    Code (markup):
    Which produces

    Which is exactly what I want. EXCEPT, In the "User" part I want to display the Username of the person who has completed that amount, ok, now your thinking idiot so just put {$off['username']}. The thing is The Usernames are not in the table, only the User ID #s, the Usernames are in their own table "Users"

    So what I need is to figure out how to pull the the Usernames from the Users Table, and Sort them in the same way as the number of completed, Now the User ID # is in both tables, Could I use that?

    I hope that makes scene, If not I can post in more detail what I need.
     
    HsHosting, Mar 25, 2009 IP
  2. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this:
    SELECT COUNT(*), completed.user_id, Users.username
     FROM completed,Users
     WHERE completed.user_id=Users.user_id
     GROUP BY completed.user_id
     ORDER BY completed.user_id DESC
    Code (markup):
    this should give you the fields:
    count user_id and username
     
    emed, Mar 26, 2009 IP
  3. HsHosting

    HsHosting Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your help, That gets it to do half of what I want

    I used this code

    <?
    include"mysql.php";
    $getoffers=mysql_query("SELECT COUNT(*), completed.user_id, users.username FROM completed,users WHERE completed.user_id=users.id GROUP BY completed.user_id ORDER BY completed.user_id DESC",$c);
    while($off=mysql_fetch_array($getoffers))
    {
    print"		
    				<li>{$off['username']} - {$off['user_id']}</li>	
    	
    ";
    }
    ?>
    Code (markup):
    and it Gives me

    User7 - 7
    User4 - 4
    User3 - 3
    User2 - 2

    It is showing the User ID # instead of the amount of times it is in that Table:confused:
     
    HsHosting, Mar 26, 2009 IP
  4. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if you want to show the number of times change SELECT COUNT(*), to SELECT COUNT(*) AS times,

    then use {$off['times']} instead of {$off['user_id']}
     
    emed, Mar 26, 2009 IP
    HsHosting likes this.
  5. HsHosting

    HsHosting Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your help, emed, I got it working!
     
    HsHosting, Mar 26, 2009 IP