한 번에 세 개의 다른 테이블에서 데이터를 가져 오려고합니다. 아무데도 빨리 가지 않으므로 올바르게 조인을 이해하지 못합니다.
예를 들어 표가 houses
, sellers
및 selling_details
인 경우를 가정 해 보겠습니다. houses
및 sellers
은 selling_details
으로 연결됩니다. 가격은 seller_id
및 house_id
이며 가격 및 사용자에 대한 링크와 같은 정보가 더 있습니다.MySQL 조인 - 3 개의 테이블에서 모든 데이터 가져 오기
시스템의 모든 집을 반환하고 모든 판매자에게 매칭되며 판매 세부 정보가 있으면 목록을 작성하는 쿼리를 작성하고자합니다. 예 :
+------------+-------------+-------------------------+----------------------+-----------+
| 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 |
+------------+-------------+-------------------------+----------------------+-----------+
가장 쉬운 방법은 무엇입니까?
편집 : 단지에 지금
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`)
: 내가 그래서 여기에 좀 더 세부의 문제를 지나치게 단순화하기 위해 노력하고있어 보인다 여기
내가 사용하고 스키마의 일부분이다 너무 복잡해지기 때문에 house
과 seller
을 연결하는 selling_details
테이블에 많은 행이있을 수 있습니다. 하나 이상의 행이있는 경우 가장 최근의 행만 필요합니다. date
; 그러한 행이 없으면 위에 나온 예의 결과와 같이 집과 판매자 조합을 반환합니다. 크로스 집 및 판매자에 가입
에 대한 모든 세부 정보를 찾기 위해 가입 떠났다. – wallyk
게시 할 때 약간 바빴지만 정보를 업데이트했습니다. 나는 그 문제를 조금 지나치게 단순화했다. –