Jump to content

[SOLVED] PHP CASE WHEN HELP


Skipjackrick

Recommended Posts

The following code is counting the species_id for every single entry in my DB.  I only want it to count the entries for each specific angler.

 

Why won't the following code work like I want?

 

 

<?php
$query_anglertotals = "SELECT
                         species_id,
                         COUNT(CASE WHEN angler = 'Oz' THEN species_id ELSE 0 END ) AS anglerA,
                         COUNT(CASE WHEN angler = 'Skipjack' THEN species_id ELSE 0 END ) AS anglerB,
                         COUNT(CASE WHEN angler = 'Curmit' THEN species_id ELSE 0 END ) AS anglerC,
                         COUNT(CASE WHEN angler = 'Old Salt' THEN species_id ELSE 0 END ) AS anglerD,
                         COUNT(CASE WHEN angler = 'Kip' THEN species_id ELSE 0 END ) AS anglerE
                         FROM submit
                         GROUP BY species_id
                         ORDER BY species_id";

$anglertotals = mysql_query($query_anglertotals) or die(mysql_error());
?>

Link to comment
Share on other sites

Nope, I tried the following and I can't get it to work.

 

Basically I am trying to arrange my data in columns rather than rows.

 

<?php
$query_anglertotals = "SELECT
                         species_id,
                         COUNT(species_id) AS anglerA FROM submit WHERE angler='Oz'
                         GROUP BY species_id
                         ORDER BY species_id
                         COUNT(species_id) AS anglerB FROM submit WHERE angler='Skipjack'
                         GROUP BY species_id
                         ORDER BY species_id
                         COUNT(species_id) AS anglerC FROM submit WHERE angler='Curmit'
                         GROUP BY species_id
                         ORDER BY species_id
                         COUNT(species_id) AS anglerD FROM submit WHERE angler='Old Salt'
                         GROUP BY species_id
                         ORDER BY species_id
                         COUNT(species_id) AS anglerE FROM submit WHERE angler='Kip'
                         GROUP BY species_id
                         ORDER BY species_id";

$anglertotals = mysql_query($query_anglertotals) or die(mysql_error());

while($row = mysql_fetch_array($anglertotals))
{
$anglerA = $row['anglerA'];
$anglerB = $row['anglerB'];
$anglerC = $row['anglerC'];
$anglerD = $row['anglerD'];
$anglerE = $row['anglerE'];
$species_id = $row['species_id'];

get_species($species_id);

$kayakwars_totals2 .=<<<EOD
<tr>
	<td align='center'>$speciesname</td>
	<td align='center'>$anglerA</td>
	<td align='center'>$anglerB</td>
	<td align='center'>$anglerC</td>
	<td align='center'>$anglerD</td>
	<td align='center'>$anglerE</td>
</tr>
EOD;
}
?>

Link to comment
Share on other sites

Ok. Give me your database layout and how you want your data to be formatted. You're trying to do way too much.

 

LOL...

 

Thanks

 

I'll PM it to you.

Not helpful the rest of us... post on this thread, please.  Besides, what output did you get from the first query you posted?

Link to comment
Share on other sites

Not helpful the rest of us... post on this thread, please.  Besides, what output did you get from the first query you posted?

 

 

 

Basically I am trying to arrange data across columns with a different "Group By" for each column.

 

Is it possible to Fetch more than one array yet, put it in the same exact table????

 

 

Case #1

 

<?php
$query_anglertotals = "SELECT
                         species_id,
                         COUNT(CASE WHEN angler = 'Oz' THEN species_id ELSE 0 END ) AS anglerA,
                         COUNT(CASE WHEN angler = 'Skipjack' THEN species_id ELSE 0 END ) AS anglerB,
                         COUNT(CASE WHEN angler = 'Curmit' THEN species_id ELSE 0 END ) AS anglerC,
                         COUNT(CASE WHEN angler = 'Old Salt' THEN species_id ELSE 0 END ) AS anglerD,
                         COUNT(CASE WHEN angler = 'Kip' THEN species_id ELSE 0 END ) AS anglerE
                         FROM submit
                         GROUP BY species_id
                         ORDER BY species_id";

$anglertotals = mysql_query($query_anglertotals) or die(mysql_error());
?>

 

The first Case I posted gives me this..............Look at the Total catches by anglers table

 

It gives me the total for each species of all of the entires.  Well, Oz did not catch 108 redfish and neither did Skipjack.

 

Click Here to See Case #1

 

 

 

Case #2

 

When I did this Oz is catching the correct amount of redfish but nobody else is catching anything.

 

 

Click Here to See Case #2

 

<?php
$query_anglertotals = "SELECT
                         species_id,
                         COUNT(species_id) AS anglerA
                         FROM submit
                         WHERE angler='Oz'
                         GROUP BY species_id
                         ORDER BY species_id";

$anglertotals = mysql_query($query_anglertotals) or die(mysql_error());
?>

 

Link to comment
Share on other sites

Just so you can see how I am fetching my array I thought I'd include my thinking process behind it.

 

 

<?php
while($row = mysql_fetch_array($anglertotals))
{
$anglerA = $row['anglerA'];
$anglerB = $row['anglerB'];
$anglerC = $row['anglerC'];
$anglerD = $row['anglerD'];
$anglerE = $row['anglerE'];
$species_id = $row['species_id'];

get_species($species_id);

$kayakwars_totals2 .=<<<EOD
<tr>
	<td align='center'>$speciesname</td>
	<td align='center'>$anglerA</td>
	<td align='center'>$anglerB</td>
	<td align='center'>$anglerC</td>
	<td align='center'>$anglerD</td>
	<td align='center'>$anglerE</td>
</tr>
EOD;
}
?>

Link to comment
Share on other sites

Question... what about making a column in your table to hold this data. It could be a tally of how many you have. And then every time a new one is added, just increment it. It could make for a much easier query.

 

Well, I should be able to figure out some matter of accomplishing what I want to do without editing the database.

 

I just haven't quite figured it out.  I am certain that someone else has tried to arrange data in columns rather than rows and MySQL probably addressed that at some point.

 

My problem is that I don't know the functions?????????  Ahhh!!!

Link to comment
Share on other sites

You want

 

SUM(IF angler = 'Oz', 1, 0 ) AS anglerA

 

not

 

COUNT(CASE WHEN angler = 'Oz' THEN species_id ELSE 0 END ) AS anglerA

 

 

 

HOLY CRAP!!!!!!!!!  What a way to use SUM and IF together!!!!!!!!!  You are amazing!!!!!!!!!!!!!!!!!!!

 

I had figured out how to do it but I used about 100 different queries to fill each row and column separately.  This simplifies it into 1 query!!!!!!!!!!!!!!

 

 

THANKS A BILLION TIMES OVER!!!!!!!!!!!!!!!

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.