[SOLVED] Get sum of more then one table? MySQL...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
iamkoa
Forum Newbie
Posts: 11
Joined: Sun Feb 06, 2005 11:36 pm

[SOLVED] Get sum of more then one table? MySQL...

Post 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.
Last edited by iamkoa on Wed Mar 02, 2005 6:44 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to database.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

What do you see if you add this statement after the fetch is done?

Code: Select all

print_r($row2);
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;

};
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

*cough* OR *cough*

...excuse me! :)
iamkoa
Forum Newbie
Posts: 11
Joined: Sun Feb 06, 2005 11:36 pm

Post 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"");
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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
iamkoa
Forum Newbie
Posts: 11
Joined: Sun Feb 06, 2005 11:36 pm

Post 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!
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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.
Post Reply