강의 컨설팅 트레이닝 무료진단 무료책자 마케팅편지 마케팅정보공유 다이어리 서비스제휴 고객센터

13.1.7.1. JOIN Syntax____SELECT
작성자 : 99 단국강토
등록날짜 : 2009.01.08 10:07
2,209

SELECT

 

13.1.7.1. JOIN Syntax

MySQL supports the following JOIN syntaxes for the table_references part of SELECT statements and multiple-table DELETE and UPDATE statements:

<PRE class=programlisting>table_reference, table_referencetable_reference [INNER | CROSS] JOIN table_reference [join_condition]table_reference STRAIGHT_JOIN table_referencetable_reference LEFT [OUTER] JOIN table_reference join_conditiontable_reference NATURAL [LEFT [OUTER]] JOIN table_reference{ OJ table_reference LEFT OUTER JOIN table_reference ON conditional_expr }table_reference RIGHT [OUTER] JOIN table_reference join_conditiontable_reference NATURAL [RIGHT [OUTER]] JOIN table_reference</PRE>

table_reference is defined as:

<PRE class=programlisting>tbl_name [[AS] alias] [[USE INDEX (key_list)] | [IGNORE INDEX (key_list)] | [FORCE INDEX (key_list)]]</PRE>

join_condition is defined as:

<PRE class=programlisting>ON conditional_expr | USING (column_list)</PRE>

You should generally not have any conditions in the ON part that are used to restrict which rows you want in the result set, but rather specify these conditions in the WHERE clause. There are exceptions to this rule.

Note that INNER JOIN syntax allows a join_condition only from MySQL 3.23.17 on. The same is true for JOIN and CROSS JOIN only as of MySQL 4.0.11.

The { OJ ... LEFT OUTER JOIN ...} syntax shown in the preceding list exists only for compatibility with ODBC.

  • A table reference can be aliased using tbl_name AS alias_name or tbl_name alias_name:

    <PRE class=programlisting>mysql> SELECT t1.name, t2.salary FROM employee AS t1, info AS t2 -> WHERE t1.name = t2.name;mysql> SELECT t1.name, t2.salary FROM employee t1, info t2 -> WHERE t1.name = t2.name;</PRE>
  • The ON conditional is any conditional expression of the form that can be used in a WHERE clause.

  • If there is no matching record for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find records in a table that have no counterpart in another table:

    <PRE class=programlisting>mysql> SELECT table1.* FROM table1 -> LEFT JOIN table2 ON table1.id=table2.id -> WHERE table2.id IS NULL;</PRE>

    This example finds all rows in table1 with an id value that is not present in table2 (that is, all rows in table1 with no corresponding row in table2). This assumes that table2.id is declared NOT NULL. See Section 7.2.9, “How MySQL Optimizes LEFT JOIN and RIGHT JOIN.

  • The USING (column_list) clause names a list of columns that must exist in both tables. The following two clauses are semantically identical:

    <PRE class=programlisting>a LEFT JOIN b USING (c1,c2,c3)a LEFT JOIN b ON a.c1=b.c1 AND a.c2=b.c2 AND a.c3=b.c3</PRE>
  • The NATURAL [LEFT] JOIN of two tables is defined to be semantically equivalent to an INNER JOIN or a LEFT JOIN with a USING clause that names all columns that exist in both tables.

  • INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table are joined to all rows in the second table).

  • RIGHT JOIN works analogously to LEFT JOIN. To keep code portable across databases, it's recommended to use LEFT JOIN instead of RIGHT JOIN.

  • STRAIGHT_JOIN is identical to JOIN, except that the left table is always read before the right table. This can be used for those (few) cases for which the join optimizer puts the tables in the wrong order.

As of MySQL 3.23.12, you can give hints about which index MySQL should use when retrieving information from a table. By specifying USE INDEX (key_list), you can tell MySQL to use only one of the possible indexes to find rows in the table. The alternative syntax IGNORE INDEX (key_list) can be used to tell MySQL to not use some particular index. These hints are useful if EXPLAIN shows that MySQL is using the wrong index from the list of possible indexes.

From MySQL 4.0.9 on, you can also use FORCE INDEX. This acts likes USE INDEX (key_list) but with the addition that a table scan is assumed to be very expensive. In other words, a table scan is only used if there is no way to use one of the given indexes to find rows in the table.

USE KEY, IGNORE KEY, and FORCE KEY are synonyms for USE INDEX, IGNORE INDEX, and FORCE INDEX.

Note: USE INDEX, IGNORE INDEX, and FORCE INDEX only affect which indexes are used when MySQL decides how to find rows in the table and how to do the join. They do not affect whether an index is used when resolving an ORDER BY or GROUP BY.

Some join examples:

<PRE class=programlisting>mysql> SELECT * FROM table1,table2 WHERE table1.id=table2.id;mysql> SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id;mysql> SELECT * FROM table1 LEFT JOIN table2 USING (id);mysql> SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id -> LEFT JOIN table3 ON table2.id=table3.id;mysql> SELECT * FROM table1 USE INDEX (key1,key2) -> WHERE key1=1 AND key2=2 AND key3=3;mysql> SELECT * FROM table1 IGNORE INDEX (key3) -> WHERE key1=1 AND key2=2 AND key3=3;</PRE>

See Section 7.2.9, “How MySQL Optimizes LEFT JOIN and RIGHT JOIN.

[출처] modestia2004

"쇼핑몰·홈페이지·오픈마켓
블로그·페이스북·이메일 등의 각종 마케팅 글쓰기,
각종 광고, 영업, 판매, 제안서, 전단지
반응율 3배×10배 이상 높이는 마법의 8단계 공식"
자세히보기

Comments

번호 제목 글쓴이 날짜 조회
2745 SSH 설명 13 김영철 01.29 2795
2744 간단한 쉘 명령어 13 김영철 01.29 2226
2743 ssh 명령어 13 김영철 01.29 2401
2742 my-sql 테이블 생성 방법 13 김영철 01.29 3076
2741 mysql 백업 및 복원 방법 13 김영철 01.24 2712
2740 sql_덤프하기_7_10_08 99 단국강토 01.12 1874
2739 Date Types_7_7_08 99 단국강토 01.12 3541
열람중 13.1.7.1. JOIN Syntax____SELECT 99 단국강토 01.08 2210
2737 SUBSTRING(str,pos) , SUBSTRING(str FROM pos) , SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos FOR len) 99 단국강토 01.08 2900
2736 mysql함수 [숫자열, 문자열, 집계, 날짜, 기타함수...] 99 단국강토 01.07 4736
2735 sql ERROR 1045 99 단국강토 01.07 2567
2734 정규패턴, 자료백업 및 복구, MySQL 설정하기 99 단국강토 01.06 2697
2733 MySQL 시스템 데이터베이스 및 테이블 구조 이해하기 99 단국강토 01.06 2381
2732 TABLE 깨지고 맛간것 복구하기 99 단국강토 01.05 2332
2731 mysql의 최대 성능 향상 방법 99 단국강토 01.05 3563
2730 mysql FULLTEXT 검색 M 최고의하루 01.03 2832
2729 동적 쿼리 사용시 속도개선문제점 해결을 위한 방안... M 최고의하루 01.03 2486
2728 MySQL - Explain 정보보는법 99 단국강토 01.02 2439
2727 해당 문자의 Index 값알아 보자 99 단국강토 01.02 2281
2726 [SQL 쿼리문] 게시판에서 글쓰기와 답글을 저장할때 99 단국강토 12.30 2938
2725 EXPLAIN [mysql] 99 단국강토 12.30 2290
2724 unsigned M 최고의하루 12.26 2429
2723 우편번호 데이터 M 최고의하루 12.26 2786
2722 Mysql 문자셋 M 최고의하루 12.24 2383
2721 mysql 에서 utf-8 로 생성하기 M 최고의하루 12.24 2681
2720 utf-8 세팅및 한글깨짐방지 M 최고의하루 12.23 4615
2719 4.1 버전의 collation M 최고의하루 12.23 2430
2718 mysqladmin정리 M 최고의하루 12.20 2177
2717 카테고리4단 inner join 이해하기 M 최고의하루 12.20 2543
2716 MySQL-Front 3.2 설치 M 최고의하루 12.19 3230
마케팅
특별 마케팅자료
다운로드 마케팅자료
창업,경영
기획,카피,상품전략
동기부여,성취