views:

56

answers:

1

What's the best way to query one-to-many in MySQL? This is a simplified version of the database I am working on (if anything doesn't look right tell me):

CREATE TABLE Tenant(  
    tenant_id int NOT NULL,  
    first_name varchar(20),  
    last_name varchar(20),  
    PRIMARY KEY (tenant_id)  
);

CREATE TABLE Rent(  
    tenant_id int NOT NULL,
    month enum('JAN', 'FEB', ...),   
    date_paid date NOT NULL,  
    amount_paid int NOT NULL,  
    FOREIGN KEY (tenant_id) REFERENCES Tenant(tenant_id)  
);  

(The reason that there is month and date_paid in the Rent table is because the tenant does not necessarily pay the rent all at once). What I want the tenant's name to appear once which would just be a Left Join, but I want all the amount paid in a particular month listed as columns for each tenant, I am not sure how to go about that. I am not really sure how to do that since your are dealing with an unknown amount of columns, haven't touched that yet in MySQL. Or is there a better strategy? Also, how would I go about creating my own variable like MONTH-YEAR (I don't think that exists as a native variable in MySQL). Thank you!

Edit: Just to simplify it further I am using this format:

create table rent(
tenant_id int not null,
year year,
amount_paid int,
foreign key (tenant_id) references tenant(tenant_id)
);

If I understand what duffymo said below I should use group by (I know I am misunderstanding somewhere because it only shows the first example for each year):
SELECT Tenant.first_name, Rent.year, Rent.amount_paid
FROM Tenant
LEFT JOIN Rent
ON Tenant.tenant_id = Rent.tenant_id
GROUP BY year;

This is what I want the query to look like, the number under each year is the amount paid (I actually just realized it's a little bit more complex than what I how explained):

first_name 2009 2008 2007
John 500 500 NULL
Ann 1000 NULL NULL
Bob NULL 700 700

A: 

If you have MONTH and YEAR columns, you can do a GROUP BY to select amount paid broken out as you'd wish. If you have a PAID_DATE column, one way to do this would be to have a BEFORE INSERT trigger that runs when the PAID_DATE is set. That way users don't have to enter values, and data integrity can be guaranteed.

duffymo
Ok, that sounds like a good idea but I am not sure how to handle the variable amounts of columns.
MCH
The number of columns in the table don't vary; it's a one-to-many relationship. You get the variable number of results in the query you write.
duffymo
Yeah, you are right. That's what I meant, in the resulting query. I edited my original post because I am still having a bit of trouble.
MCH