Results 1 to 2 of 2

Thread: SQL Query

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    SQL Query

    Is it possible to use the below query? I can't seem to get the count for the second table.

    PHP Code:
    $sSQL mysql_query("SELECT COUNT(*) FROM users, contacts"); 
    $details mysql_fetch_array($sSQL);

    echo 
    "The total amount of users is <b>$details[0]</b>.<br>
    The total amount of contacts is <b>
    $details[1]</b>.<p>"

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: SQL Query

    That won't worrk. Firstly what your query is doing, is combining two tables users, contracters. When you do this the tables arn't appended to one another they are literally combined where ever users, contracters combination of records is returned together. So the number of records returned would be (number of records in users) * (number of records in contacters). The example below demonstrates this:
    Code:
    table1    table2
    1.1        2.1  
    1.2        2.2  
    1.3        2.3  
    1.4        2.4
    
    SELECT * FROM table1,table2
    
    result
    1.1  2.1
    1.1  2.2
    1.1  2.3
    1.1  2.4
    1.2  2.1
    1.2  2.2
    1.2  2.3
    1.2  2.4
    1.3  2.1
    1.3  2.2
    1.3  2.3
    1.3  2.4
    1.4  2.1
    1.4  2.2
    1.4  2.3
    1.4  2.4
    
    total records = 16 (4 * 4)
    Back to your problem, there are two ways around it, you can either execute two separate queries or use SQL to return a table containing two rows. One with the number of rows from the users table an one with the number of rows from the contracters table:
    Code:
    (SELECT COUNT(*) FROM users) UNION ALL (SELECT COUNT(*) FROM contracters);
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width