NYCPHP Meetup

NYPHP.org

[nycphp-talk] SQL COUNT() Question

Daniel Kushner nyphp at websapp.com
Sat Feb 14 13:12:57 EST 2004


If you're going to use MYSQL, this is a little [syntax] trick I learnt some
time ago:

SELECT
SUM(win = $team) as w,
SUM(loss = $team) as l
FROM games

--Daniel

> SELECT
> SUM( IF ( win = $team, 1, 0 ) )  AS w,
> SUM( IF ( loss = $team, 1, 0 ) )  AS l
> FROM games
> 
> 
> On Feb 13, 2004, at 6:08 PM, Adam Maccabee Trachtenberg wrote:
> 
> > I am trying to generate some statistics based on basketball 
> results. I 
> > have a table that stores the id of the winning team and the losing 
> > team, like so:
> >
> > --------------
> > | win | loss |
> > --------------
> > |   1 |    2 |
> > |   1 |    3 |
> > |   2 |    3 |
> > --------------
> >
> > I want to find the total number of wins and loses for a 
> given team. I 
> > can do this with two queries, like so:
> >
> > SELECT COUNT(*) AS w FROM games WHERE win = $team; SELECT 
> COUNT(*) AS 
> > l FROM games WHERE loss = $team;
> >
> > Is there anyway I can do this in a single query? I'm actually less 
> > interested in the win and loss totals than a winning percentage, so 
> > I'd really prefer to just make the database return that 
> number to me 
> > instead of requiring me to calculate this in PHP.
> >
> > Right now, I'm using SQLite, but I'd switch to MySQL if some 
> > MySQL-specific function (like IF()?) could help me out and 
> there's no 
> > way to do this in standard SQL92.
> >
> > I feel that maybe I need to use a self JOIN, but I can't 
> get that to 
> > pan out.
> >
> > -adam
> >
> > --
> > adam at trachtenberg.com
> > author of o'reilly's php cookbook
> > avoid the holiday rush, buy your copy today!
> > _______________________________________________
> > talk mailing list
> > talk at lists.nyphp.org
> > http://lists.nyphp.org/mailman/listinfo/talk
> 
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
> 



>From hans not junk at nyphp.com  Sat Feb 14 13:51:48 2004
Return-Path: <hans not junk at nyphp.com>
Received: from ehost011-1.exch011.intermedia.net (unknown [64.78.21.3])
	by virtu.nyphp.org (Postfix) with ESMTP id 82582A85EA
	for <talk at lists.nyphp.org>; Sat, 14 Feb 2004 13:51:48 -0500 (EST)
X-MimeOLE: Produced By Microsoft Exchange V6.5.6944.0
Content-class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Subject: RE: [nycphp-talk] new to list -- hello
Date: Sat, 14 Feb 2004 10:51:46 -0800
Message-ID: <41EE526EC2D3C74286415780D3BA9F87937131 at ehost011-1.exch011.intermedia.net>
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
Thread-Topic: [nycphp-talk] new to list -- hello
Thread-Index: AcPygL9r77046A8qTeSaw2KI2+GIwQAqpW4w
From: "Hans Zaunere" <hans not junk at nyphp.com>
To: "NYPHP Talk" <talk at lists.nyphp.org>
X-BeenThere: talk at lists.nyphp.org
X-Mailman-Version: 2.1.2
Precedence: list
Reply-To: NYPHP Talk <talk at lists.nyphp.org>
List-Id: NYPHP Talk  <talk.lists.nyphp.org>
List-Unsubscribe: <http://lists.nyphp.org/mailman/listinfo/talk>,
	<mailto:talk-request at lists.nyphp.org?subject=unsubscribe>
List-Archive: <http://lists.nyphp.org/pipermail/talk>
List-Post: <mailto:talk at lists.nyphp.org>
List-Help: <mailto:talk-request at lists.nyphp.org?subject=help>
List-Subscribe: <http://lists.nyphp.org/mailman/listinfo/talk>,
	<mailto:talk-request at lists.nyphp.org?subject=subscribe>
X-List-Received-Date: Sat, 14 Feb 2004 18:51:49 -0000


Hi Allen,

> I just thought since I'm new here it would be polite to say hello.  =
I've
> been tinkering with PHP for a few years, to the point now where I =
would even
> call it "programming".  Currently working for a non-profit in =
Westchester
> County, mostly managing a database which we moved to the Internet 3 =
months
> ago (from MS Access) now using PHP/MySQL on RedHat 7.3

This is great... I've working on a couple projects similar to this, and =
it's nice to see these types of moves.

> Thanks for having me.  I enjoy the reading already, and if I ever have
> anything to say I hope it's useful.

Nice to have you.

Best regards,

---
Hans Zaunere
President
New York PHP
http://nyphp.org





More information about the talk mailing list