MySQL Query help please!
| From: | Griffiths, Daniel | Date: | Wed, 12 Mar 2003 15:02:58 +0000 |
| Subject: | MySQL Query help please! | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-26282@lists.php.net to get a copy of this message | ||
Hi all,
I have 2 tables that I need to extract data from, say PORTS and STATS.
PORTS contains a list of Sea Ports in PORT i.e. : -
PORTS.PORT
--------------------
UK
FRANCE
GERMANY
NETHERLANDS
SPAIN
ITALY
STATS contains a few thousand records that with among other things has the elements TO_PORT and
AMOUNT.
the entries in TO_PORT match the ports in PORT.
Now one of the things I want to do is produce a summary of the totals of AMOUNT against the ports in
PORTS, including (and this is the important bit that I'm stuck on) the ports in PORTS that do
not have a match in STATS.TO_PORT. so that I get a result such as : -
UK 300
FRANCE 100
GERMANY 400
NETHERLANDS 0
SPAIN 0
ITALY 300
and NOT :-
UK 300
FRANCE 100
GERMANY 400
ITALY 300
Which is all I can get at the moment.
the query I am using is : -
SELECT PORTS.PORT, SUM(STATS.AMOUNT) FROM PORTS LEFT JOIN STATS ON PORTS.PORT = STATS.TO_PORT GROUP
BY PORTS.PORT
Basicaly what I want to do is pull out a list of all entries in PORTS.PORT and put a total figure
against it from STATS.AMOUNT where the STATS.TO_PORT matches PORTS.PORT and just a zero if theres no
entry in STATS.
Anyone got any ideas?
Thanks
Dan