Re: help with complex query
| From: | Daniel Barton | Date: | Mon, 12 Nov 2001 02:50:57 +0000 |
| Subject: | Re: help with complex query | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-14240@lists.php.net to get a copy of this message | ||
Hi,
Using if() in the select statement is a useful tool/toy. You can create
pretty powerful cross-tabulations from one or multiple tables when using it in
combonation with the sum() command. Thus:
select sum(if(<someconidition>,1,0) as <somefield>,
sum(if(<somecondition>,1,0) as <somefield>, etc...
from <some table>
where <whatever>
group by <however you want to do this>
order by <however you want to do this>
You can tabulate things out of multiple tables grouped however you want
with this sum()/if() combination... many other functions possible.
Cheers,
db
Noah Spitzer-Williams wrote:
> hey guys, ok i have a table that looks somethign like this:
>
> 1 | js | 1.3 | 10
> 1 | js | 1.3 | 5
> 1 | js | 1.2 | 3
> 2 | browser | ie5 | 17
>
> lets say the first column refers to a web site's id and the two middle
> columns are stats about the site. the last column is how many times that
> stat occured. for example, site 2 had 17 visitors using the ie5 browser.
>
> what i want to do is make a 'global stats' page that combines all of the
> stats. it would read:
>
> js | 1.3 | 15
> js | 1.2 | 3
> browser | ie5 | 17
>
> noticed how it combined the two js 1.3 columns
>
> i've tried grouping and many other things i just cant figure out how to do
> it.
>
> any help is appreciated!
>
> - Noah
>
> www.trademytraffic.com
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
> For additional commands, e-mail: php-db-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
----------
Dan Barton
Terrestrial Program Biologist
Asst. Data Manager
Point Reyes Bird Observatory
http://www.prbo.org
dbarton@prbo.org
dbarton@grayflycatcher.org