MySQL Joins - getting all data from 3 tables -


i trying data 3 different tables @ once, , don't think quite understand joins correctly, i'm getting fast.
example, lets tables houses, sellers, , selling_details. houses , sellers linked selling_details: has seller_id , house_id, plus more information such price, , link user.

i want build query returns houses in system, matched sellers, , list selling details if present. example:

+------------+-------------+-------------------------+----------------------+-----------+ | house.name | seller.name | selling_details.details | selling_details.date | user.name | +------------+-------------+-------------------------+----------------------+-----------+ | 1        | 1         | details                 | 2011-02-18           | bobby     | | 1        | 2         | details                 | 2011-02-24           | frank     | | 1        | 3       | null                    | null                 | null      | | 1        | 4        | null                    | null                 | null      | | 2        | 1         | details                 | 2011-01-16           | ned       | | 2        | 2         | null                    | null                 | null      | | 2        | 3       | details                 | 2011-02-12           |      | | 2        | 4        | null                    | null                 | null      | +------------+-------------+-------------------------+----------------------+-----------+ 

what easiest way go this?

edit: seems i'm trying oversimplify problem here's more detail:

here's fraction of 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, make things complicated too, there may many rows in selling_details table linking house , seller. if there 1 or more of these rows present, want 1 recent date; if there's no such row, still return house , seller combination in example results above.

cross join house , sellers, left join find details combination

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

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -