MySQL Query help please!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux