Joining two MySQL tables
| From: | Brian T. Allen | Date: | Sun, 05 Nov 2000 02:45:13 +0000 |
| Subject: | Joining two MySQL tables | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-23757@lists.php.net to get a copy of this message | ||
Hi *,
I am sure there is probably a simple solution to this, but it is eluding me...
I have two tables:
CREATE TABLE A (
ID varchar(8) NOT NULL,
AMOUNT decimal(8,2) DEFAULT '0.00' NOT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE B (
ID int(8) NOT NULL auto_increment,
A_ID varchar(8) NOT NULL,
USED decimal(8,2) DEFAULT '0.00' NOT NULL,
PRIMARY KEY (ID),
);
Table A is for credits on an account, Table B is for purchases made against those credits.
I want to generate a list of all credits and their balance, including those that haven't been
used at all:
SELECT A.ID, A.AMOUNT, sum(B.USED) AS B_USED FROM A,B WHERE A.ID = B.A_ID GROUP BY A.ID
This query works great except that it only lists credits from Table A that have a corresponding
listing (or multiple records) in Table B.
Any suggestions? Is there a type of join for this?
Thanks,
Brian