Page 1 of 1
[SOLVED] Get sum of more then one table? MySQL...
Posted: Wed Mar 02, 2005 1:32 pm
by iamkoa
I'm trying to get the sum of two numbers in two different tables, located in the same column.
[title] - [count]
blah_high.mp3 - 67
blah_low.mp3 - 17
I want the totals from those two above... this is what I have:
Code: Select all
$fetch_song = mysql_query("SELECT title FROM band_songs WHERE `id`='$id'");
while(list($title) = mysql_fetch_array($fetch_song)){
$fetch_download = mysql_query("SELECT SUM(count) AS sum FROM band_count WHERE `file`="'$title'_high.mp3" AND `file`="'$title'_low.mp3"");
$row2 = mysql_fetch_array($fetch_download);
$blah = $row2їsum];
echo $blah;
};
Doesn't work. $blah should equal 84... though it echos nothing.
Posted: Wed Mar 02, 2005 1:33 pm
by John Cartwright
Moved to database.
Posted: Wed Mar 02, 2005 2:30 pm
by smpdawg
What do you see if you add this statement after the fetch is done?
e.g.
Code: Select all
$fetch_song = mysql_query("SELECT title FROM band_songs WHERE `id`='$id'");
while(list($title) = mysql_fetch_array($fetch_song)){
$fetch_download = mysql_query("SELECT SUM(count) AS sum FROM band_count WHERE `file`="'$title'_high.mp3" AND `file`="'$title'_low.mp3"");
$row2 = mysql_fetch_array($fetch_download);
print_r($row2);
$blah = $row2їsum];
echo $blah;
};
Posted: Wed Mar 02, 2005 3:15 pm
by feyd
*cough* OR *cough*
...excuse me!

Posted: Wed Mar 02, 2005 6:07 pm
by iamkoa
smpdawg, inserting "print_r($row2);" gives me this value:
Code: Select all
Array ( ї0] => їsum] => )
And
feyd, using OR in place of AND gives me a blank output.
Code: Select all
$fetch_download = mysql_query("SELECT SUM(count) AS sum FROM band_count WHERE `file`="'$title'_high.mp3" OR `file`="'$title'_low.mp3"");
Posted: Wed Mar 02, 2005 6:24 pm
by smpdawg
The query is wrong.
Code: Select all
$fetch_download = mysql_query("SELECT SUM(count) AS sum FROM band_count WHERE `file`="{$title}_high.mp3" OR `file`="{$title}_low.mp3"");
You are slipping in single quotes in the other query for the file names look like this
'file'_low.mp3
Posted: Wed Mar 02, 2005 6:41 pm
by iamkoa
When using the following, I get a blank output:
$fetch_download = mysql_query("SELECT SUM(count) AS sum FROM band_count WHERE file=\"{$title}_high.mp3\" AND file=\"{$title}_low.mp3\"");
When using OR in place of AND, everything works:
$fetch_download = mysql_query("SELECT SUM(count) AS sum FROM band_count WHERE file=\"{$title}_high.mp3\" OR file=\"{$title}_low.mp3\"");
Thanks guys for your help!
Posted: Wed Mar 02, 2005 6:49 pm
by smpdawg
AND would not work in your example because the file name would have to simultaneously be two different values which would be highly improbable.
OR works because it only needs to match on one of the file names.