Results 1 to 4 of 4

Thread: Count function prints the line with 0

  1. #1
    Join Date
    Nov 2008
    Posts
    1,221

    Count function prints the line with 0

    I have a table in which $row['command'] is a '0'. But when I call it, it is not zero. In fact it shows the value of the command of other.

    I know I might not be clear but my code should explain the whole incidence:
    PHP Code:
    <?php
    //Request to take the prospect and the total nb id
    $query_vg ="
    SELECT substr(customers_from, 1, 3) AS Origin,
           COUNT( customers_id) AS effectif_vg
    FROM customers
    GROUP BY Origin "
    ;
    $result_vg mysql_query($query_vg) ;
    //Request to make the prospect, nb command and the average amount command.
    $query " SELECT substr(customers_from, 1, 3) AS Origin,
           count(co.command_status) AS command,
           AVG(co.command_amount) AS amt
    FROM customers cu
    INNER JOIN command co
    ON cu.customers_id=co.customers_id
    WHERE co.command_status='1' 
    GROUP BY Origin "
    ;
    $result mysql_query($query) ;
    while (
    $row1 mysql_fetch_array($result_vg) and $row mysql_fetch_array($result) )
    {
    ?>
     <tr>
      <td style="background-color:#CCCCCC;">  </td>
                <td style="text-align:center;"> <?php echo $row1['Origin'?> </td>
       <td style="text-align:center;"> <?php echo $row1['effectif_vg'?> </td>
      <td style="text-align:center;"> <?php echo $row['command'?> </td>
                <td style="text-align:center;"> <?php echo round(($row['command']*$row['amt'])) ?> euro</td>
                  <td style="text-align:center;"> <?php echo round($row['amt'] ,2)?> </td>
     </tr> 
    <?php

    ?>

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Count function prints the line with 0

    If I understand correctly, what you have to do is replace your INNER JOIN with LEFT OUTER JOIN.

    Your Text:

    SELECT substr(customers_from, 1, 3) AS Origin,
    count(co.command_status) AS command,
    AVG(co.command_amount) AS amt
    FROM customers cu
    INNER JOIN command co
    ON cu.customers_id=co.customers_id
    WHERE co.command_status='1'
    GROUP BY Origin

    would be replaced by:

    SELECT substr(customers_from, 1, 3) AS Origin,
    count(co.command_status) AS command,
    AVG(co.command_amount) AS amt
    FROM customers cu
    LEFT OUTER JOIN command co ON cu.customers_id=co.customers_id
    AND co.command_status = '1'
    GROUP BY Origin

    You can then use CASE ... WHEN ... THEN ... if you want to display a text like "No command" for people whose COUNT (...) would be 0.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Count function prints the line with 0

    Thank you for the request but I am on the server and I have to reboot the server if I do that changes. I think I did a mistake doing this on the server directly.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Count function prints the line with 0

    Is it a MySQL database?

    In any case, I personally try my queries on databases that I access, and I still have not managed to test on a server with a request of this type.

    May be trying to test locally before launching a server, but I really do not see what could reboot the server.

Similar Threads

  1. Replies: 6
    Last Post: 25-02-2011, 10:40 AM
  2. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  3. How can I use the count() function in CPP
    By Garett in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 09:07 PM
  4. Count function help in excel
    By Logan 2 in forum Windows Software
    Replies: 5
    Last Post: 30-12-2009, 12:15 PM
  5. How to let Java read text file line by line.
    By Visala28 in forum Software Development
    Replies: 3
    Last Post: 04-08-2009, 11:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,711,262.21496 seconds with 16 queries