tags:

views:

93

answers:

4

Hi there,

i am using the following code to count, and to sum the values from the database.

$query = "SELECT
          COUNT(n.*) AS cnt_news,
          COUNT(a.*) AS cnt_adv,
          COUNT(c.*) AS cnt_comm,
          SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_approved,
          SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_unapproved,
          SUM(CASE WHEN c.spam = '0' THEN 1 ELSE 0 END) AS cnt_spam,
          SUM(a.amount) AS t_amnt,
          SUM(a.cashpaid) AS t_cpaid,
          SUM(a.balance) AS t_bal
          FROM
          news n, advertisements a, comments c";
          $result = mysql_query($query) or die(mysql_error());
          $row = mysql_fetch_array($result);

the following code gives me an error, the error is

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS cnt_news, COUNT(a.*) AS cnt_adv, COUNT(c.*) AS cnt_c' at line 2

if i remove the first three lines of the select query, it does not show the error instead it prints the wrong values.

that is wrong with my code. ??

the following code works perfectly fine for me.

$query = "SELECT COUNT(*) as cnt_news FROM news";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT COUNT(*) as cnt_adv FROM advertisements";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$query = "SELECT COUNT(*) as cnt_comm FROM comments";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_approved,
          SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS cnt_unapproved,
          SUM(CASE WHEN c.spam = '1' THEN 1 ELSE 0 END) AS cnt_spam
          FROM COMMENTS c";
$result = mysql_query($query);
$row = mysql_fetch_array($result);


$query = "SELECT SUM(a.amount) as t_amnt,
          SUM(a.cashpaid) as t_cpaid,
          SUM(a.balance) as t_bal
          FROM advertisements a";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

where am i going wrong?

A: 

It looks like Mysql doesn't like that line. Change COUNT(n.*) to COUNT(n.id) or whatever the name of that table's primary key field is. Do the same for a and c.

Fanis
i tried your way the error vanishes but why the hell it prints the wrong values? i have two number of rows in the database table and it is showing 30 instead. :(
Ibrahim Azhar Armar
You're doing a full join against the three tables news n, advertisements a, comments c... try replacing COUNT(n.id) (and equivalent for each of your tables) with COUNT(DISTINCT n.id)
Mark Baker
@Mark Baker good catch, I didn't even notice the rest of the query. Are the 3 tables even joined on any column? I presume news have comments and advertisements.
Fanis
@Mark Baker your code works for first three queries i used the code. COUNT(DISTINCT n.id) AS cnt_news, and this works perfectly fine, however if i add distinct to the fourth query it still gives the wrong value. ?
Ibrahim Azhar Armar
There's no join of any kind in the original set of queries that's being amalgamated either, but the best answer would be to do an appropriate join (requires more input from the OP to help identify the joins) rather than to use DISTINCT
Mark Baker
If i use Distinct in the seventh query SUM(DISTINCT a.amount) AS t_amnt, it just takes the values from the first two rows and ignored the rest.
Ibrahim Azhar Armar
@Mark i have put the working code with number of queries already, how do i make the proper join for the tables?
Ibrahim Azhar Armar
@Ibrahim To advise you on the joins, we need to know the relationships between the tables (e.g. primary and foreign keys, etc)
Mark Baker
@Mark I have updated the post, please take a look. ty
Ibrahim Azhar Armar
A: 

You cannot use count(tablename.*), try using count(tablename.columnname)

JohnoBoy
i tried changing to COUNT(n.id) AS cnt_news, ut works but prints out the wrong values,.. any idea why?
Ibrahim Azhar Armar
If the column you specified has NULL values, the count command will ignore them, try using the count on the id column of the table or a non-null column
JohnoBoy
A: 

You can try

SELECT (SELECT COUNT(*) FROM news) AS cnt_news, (SELECT COUNT(*) FROM advertisements) as cnt_adv, ...

viriathus
A: 

Well i dropped the idea of making my queries into a single one, and as suggested by Col.Shrapnel i did a custom function for it, and i found it very easy to maintain the code this way. thank you Col.Sharpnel i am posting the answer suggested by him.

this is the user defined function i created.

function dbgetvar($query) {
             $res = mysql_query($query);
         if( !$res) {
             trigger_error("dbget: ". mysql_error(). " in " .$query);
             return false;
             }
             $row = mysql_fetch_array($res);
             if(!$row) return "";
             return $row;
             }  

and then i called my function using this code.

     $news = dbgetvar("SELECT COUNT(*) as count FROM news");
 $comments = dbgetvar("SELECT SUM(CASE WHEN c.approve = '1' AND c.spam = '0' THEN 1 ELSE 0 END) AS approved,
                       SUM(CASE WHEN c.approve = '0' AND c.spam = '0' THEN 1 ELSE 0 END) AS pending,
                       SUM(CASE WHEN c.spam = '1' THEN 1 ELSE 0 END) AS spam,
                       COUNT(*) AS count
                       FROM COMMENTS c");
$advertise = dbgetvar("SELECT SUM(a.amount) AS amount,
                       SUM(a.cashpaid) AS cashpaid,
                       SUM(a.balance) AS balance,
                       COUNT(*) AS count
                       FROM advertisements a");

the above code is working perfectly fine for me.

Ibrahim Azhar Armar