나를 세 가지 답변이 참으로 올바른 이유를 설명하자,이 특정 질문에 대한 메타에 your question를 읽고 그것을 밖으로 ked.
는
나는 그들이 작업하는 세 가지 답변 스키마의 예를 포함했다 :
Database changed
mysql> create table carpet(id int(3), material varchar(10), color varchar(15));
Query OK, 0 rows affected (0.02 sec)
mysql> create table curtain(id int(3), material varchar(10), color varchar(15));
Query OK, 0 rows affected (0.00 sec)
은 (insert 문의 무리)
mysql> select * from carpet;
+------+-----------+--------------+
| id | material | color |
+------+-----------+--------------+
| 1 | wool | Light Yellow |
| 2 | wool | Beige |
| 3 | polyester | Light Yellow |
| 4 | polyester | Light Red |
+------+-----------+--------------+
4 rows in set (0.00 sec)
mysql> select * from curtain;
+------+----------+--------------+
| id | material | color |
+------+----------+--------------+
| 1 | Velvet | Purple |
| 2 | cotton | White |
| 3 | cotton | Light Yellow |
| 4 | cotton | Light Blue |
+------+----------+--------------+
4 rows in set (0.00 sec)
교집합은 두 개의 선택 문을 사용하여 다시 제공 일치하는 결과. 이 경우 '밝은 노란색'과 일치하는 색상을 가진 모든 행을 찾고 있습니다.
MySQL을 지원하지 않으므로 MySQL에서 예제를 제공 할 수 없습니다 (아래에서 볼 수 있듯이 동일한 결과를 줄 필요는 없습니다).
'밝은 노란색'의 색상 만 허용하는 where 절이있는 두 개의 select 문을 통합 검색하면 동일한 데이터가 반환됩니다. union을 사용하여 일치하지 않는 데이터를 반환 할 수는 있지만 각 select 문의 where 절은 원하는 행만 반환 할 수 있음을 의미합니다.
mysql> select id, material, color from carpet
-> union
-> select id, material, color from curtain;
+------+-----------+--------------+
| id | material | color |
+------+-----------+--------------+
| 1 | wool | Light Yellow |
| 2 | wool | Beige |
| 3 | polyester | Light Yellow |
| 4 | polyester | Light Red |
| 1 | Velvet | Purple |
| 2 | cotton | White |
| 3 | cotton | Light Yellow |
| 4 | cotton | Light Blue |
+------+-----------+--------------+
8 rows in set (0.00 sec)
아, 맞지 않니?당신은 데이터의 단일 행의 테이블 모두에서 행을 반환 할 수 있습니다
mysql> select id, material, color from carpet where color='Light Yellow'
-> union
-> select id, material, color from curtain where color='Light Yellow';
+------+-----------+--------------+
| id | material | color |
+------+-----------+--------------+
| 1 | wool | Light Yellow |
| 3 | polyester | Light Yellow |
| 3 | cotton | Light Yellow |
+------+-----------+--------------+
3 rows in set (0.00 sec)
A는 색상에 두 테이블 사이에 조인 물론, 우리는 어디에 절을 지정하지 않았습니다. 항목 색상에 대해 두 테이블의 조인을 지정하고 where 절을 사용하면 찾고있는 행만 반환 할 수 있습니다.
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a join carpet b on a.color=b.color;
+------+----------+--------------+------+-----------+
| id | material | color | id | material |
+------+----------+--------------+------+-----------+
| 3 | cotton | Light Yellow | 1 | wool |
| 3 | cotton | Light Yellow | 3 | polyester |
+------+----------+--------------+------+-----------+
2 rows in set (0.00 sec)
당신이 볼 수 있듯이
,이 일치하는 색으로 행만을 반환하고 당신이 당신의 결과 집합의 단일 행에 두 테이블에서 열을 가질 수있다.
지금, 나는이 두 테이블의 '빛 노란색'에서 떨어져 다른 일치하는 결과가 없기 때문에 나는 명확하게 아주 잘 계획하지 않았다, 그래서 몇 항목을 추가 할 경우 우리는이 수에 :
mysql> select * from curtain;
+------+----------+--------------+
| id | material | color |
+------+----------+--------------+
| 1 | Velvet | Purple |
| 2 | cotton | White |
| 3 | cotton | Light Yellow |
| 4 | cotton | Light Blue |
| 5 | Wool | White |
| 6 | Fluff | Beige |
+------+----------+--------------+
6 rows in set (0.00 sec)
mysql> select * from carpet;
+------+-----------+--------------+
| id | material | color |
+------+-----------+--------------+
| 1 | wool | Light Yellow |
| 2 | wool | Beige |
| 3 | polyester | Light Yellow |
| 4 | polyester | Light Red |
| 5 | Fluff | Light Blue |
+------+-----------+--------------+
5 rows in set (0.00 sec)
을
이제 우리는 다시 실행할 수 있습니다,이 시간을 얻을 :
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a join carpet b on a.color=b.color;
+------+----------+--------------+------+-----------+
| id | material | color | id | material |
+------+----------+--------------+------+-----------+
| 3 | cotton | Light Yellow | 1 | wool |
| 3 | cotton | Light Yellow | 3 | polyester |
| 4 | cotton | Light Blue | 5 | Fluff |
| 6 | Fluff | Beige | 2 | wool |
+------+----------+--------------+------+-----------+
4 rows in set (0.00 sec)
오 noes를! 같은의 변화가보다 다양한 수단을 통해 동일한 결과를 얻기 위해 종종 방법이 SQL에서,
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a join carpet b on a.color=b.color
-> where a.color='Light Yellow';
+------+----------+--------------+------+-----------+
| id | material | color | id | material |
+------+----------+--------------+------+-----------+
| 3 | cotton | Light Yellow | 1 | wool |
| 3 | cotton | Light Yellow | 3 | polyester |
+------+----------+--------------+------+-----------+
2 rows in set (0.00 sec)
당신은 참조 : 우리는이 조인을 사용 함께 WHERE 절 어디
이
은 지금 귀하의 테이블에있는 데이터. 당신은 단지
데이터가 일치하는 모든, 그냥 조인 구문에 포함 된 행을 원한다면, 그래서 좋아, :
편집을 당신이 볼 수 있듯이
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a
-> join carpet b on a.color=b.color
-> and a.id=b.id
-> where a.color='Light Yellow';
+------+----------+--------------+------+-----------+
| id | material | color | id | material |
+------+----------+--------------+------+-----------+
| 3 | cotton | Light Yellow | 3 | polyester |
+------+----------+--------------+------+-----------+
1 row in set (0.00 sec)
는 지금 우리가 말하는 모두가 가입 id
및 color
필드는 두 테이블간에 일치해야하며 그 결과는 직접 말해야합니다. 이제,이 경우, 기술적으로 이 모든 열과 일치하지 않습니다. 재료가 다르기 때문입니다. 그 노트에
mysql> select a.id, a.material, a.color, b.id, b.material
-> from curtain a
-> join carpet b on a.color=b.color
-> and a.id=b.id
-> and a.material=b.material
-> where a.color='Light Yellow';
Empty set (0.00 sec)
을하지만, 당신 : 당신이 더 일치하고 싶었다면 내가 아이디, 재료 및 색상 일치하지만 같은 것 구문은 다음과 일치하는 기록이 없기 때문에, 쿼리 결과를 반환하지 않을 대부분의 경우 모두 일치하는 열을 원하지 않습니다. 대개 테이블에는 해당 테이블에서만 사용되는 ID가 있으며 자동으로 증가하는 값입니다. 의 고유 한 행을 식별하는 데이 테이블을 사용하여 관련 테이블과 일치시키는 데 사용하지 않으려면 테이블을 사용하려고합니다. 어떤 것이 든, 나는 당신이 물질과 색깔에 매치 될 것을 제안했지만, ID는 그대로 두었다.
샘플 데이터에서 원하는 것은 id = 3뿐입니다. id = 1 인 경우 카펫 색상 = "밝은 노란색"의 커튼 색상 = "보라색"이기 때문에 id = 1이 없습니다. – Celeritas
@Celeritas Coming right up : – Fluffeh
@Celeritas 그리고 편집을 참조하십시오. – Fluffeh