Jump to content

getting result from a select sum if query


kapkap
Go to solution Solved by kapkap,

Recommended Posts

Hi there

I am new to this forum and not much of a php expert. I hope you can assist me.

My problem is as follows:

I have a column consisting of several values (in this case: sceience, art, humanistics and music)

I would like to use the following mysqli query in order to count the appearances of each value in this column:

 

 

$query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0))-> AS art FROM new2"

 

I have tried lots of ways to loop the results but couldn't get the it working.

Can anyone suggest any idea?

 

Thanks

 

Hanan

 

Link to comment
Share on other sites

 

$query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0))-> AS art FROM new2"

 

Is this the query string trying to execute by php to mysql? if so, that the syntax of your query is not correct.

Edited by jazzman1
Link to comment
Share on other sites

Try changing this:

$query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0))-> AS art FROM new2";
 
To this:
$query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0)) AS art FROM new2";
 
 
Note that I removed the "->" before " AS art".
Link to comment
Share on other sites

  • Solution

hi 

solved with help from a friend, a little different but gives me the data I need

 
$query = "select department, count(department) as count from new2
group by department";
if ($result = mysqli_query($con, $query)) {
 
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row['department'], $row['count']);
}
 
/* free result set */
mysqli_free_result($result);
}
 
mysqli_close($con);
 
thank you all
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.