开发者

MySQL Joins - getting all data from 3 tables

I am trying to get data from three different tables at once, and I don't think I quite understand joins correctly, as I'm getting nowhere fast.

For example, lets say the tables are houses, sellers, and selling_details. houses and sellers are linked by the selling_details: it has a seller_id and a house_id, plus more information such as price, and a link to a user.

I want to build a qu开发者_高级运维ery that returns all houses in the system, matched up to all sellers, and list the selling details if present. For example:

+------------+-------------+-------------------------+----------------------+-----------+
| house.name | seller.name | selling_details.details | selling_details.date | user.name |
+------------+-------------+-------------------------+----------------------+-----------+
| One        | One         | details                 | 2011-02-18           | bobby     |
| One        | Two         | details                 | 2011-02-24           | frank     |
| One        | Three       | NULL                    | NULL                 | NULL      |
| One        | Four        | NULL                    | NULL                 | NULL      |
| Two        | One         | details                 | 2011-01-16           | ned       |
| Two        | Two         | NULL                    | NULL                 | NULL      |
| Two        | Three       | details                 | 2011-02-12           | will      |
| Two        | Four        | NULL                    | NULL                 | NULL      |
+------------+-------------+-------------------------+----------------------+-----------+

What would be the easiest way to go about this?

Edit: It seems I'm trying to oversimplify the problem so here's some more detail:

Here's a fraction of the schema I'm using:

create table `house` (`id` int not null auto_increment, `name` varchar(255) null, primary key (`id`))
create table `seller` (`id` int not null auto_increment, `name` varchar(255) null, primary key (`id`))
create table `user` (`id` int not null auto_increment, `name` varchar(255) null, primary key (`id`))
create table `selling_details` (`id` int not null auto_increment, `details` varchar(255) not null, date datetime not null, `house_id` int null, `seller_id` int null, `user_id` int not null, primary key (`id`))

alter table `selling_details` add index `FK_selling_details_user` (`user_id`), add constraint `FK_selling_details_user` foreign key (`user_id`) references `user` (`id`)
alter table `selling_details` add index `FK_selling_details_house` (`house_id`), add constraint `FK_selling_details_house` foreign key (`house_id`) references `house` (`id`)
alter table `selling_details` add index `FK_selling_details_seller` (`seller_id`), add constraint `FK_selling_details_seller` foreign key (`seller_id`) references `seller` (`id`)

Now, just to make things really complicated too, there may be many rows in the selling_details table linking house and seller. If there is one or more of these rows present, I only want the one with the most recent date; if there's no such row, still return the house and seller combination as in the example results above.


Cross join the house and sellers, then left join to find any details for the combination

select h.house_id, s.seller_id, sd.details
from houses h
cross join sellers s
left join selling_details sd
    on sd.seller_id = s.seller_id
    and sd.house_id = h.house_id
order by h.house_id, s.seller_id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜