MySQL_Tables_Joins.txt
17KB · 418 lines
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
�������Ʈ: http://www.devshed.com/Server_Side/MySQL/Join/
* MySQL Table Joins
*(o) By W.J. Gilmore
*(o)| July 06, 1999
(o)| | ���� : ������(lolol@shinan.hongik.ac.kr)
=======================================================================
[����Ƿ�� �ʹ� ����ϴٺ��� �߸��� �κ�� ��� ���� �ֽ��ϴ�.
���ľ��� �κ�� ����� �� ���� �����ּ���.]
�ߡ� �Ұ��ϱ�
SQL(Structured Query Language)�� ���� ū Ư¡ ��� �ϳ��� �������� ��̺�
������� ������ �˻��ϰų� ������ �� �ִ� ���̴�. �̰�� ������ ����
������ �˻��ϰ� ���ʿ��� ������ �ٿ��ִ� ���� �ִ�. �ٸ� SQL ���� ���
������ MySQL�� join��ɾ�� �� ���� �����Ѵ�.
����� ���ϸ� join�� �ΰ� �̻�� ��̺�κ��� �ʿ��� ������ ������ �ϳ���
������� ������ ���ս�Ű�� ����̴�.
���� ���, �� ��ǻ�� �������ڰ� �ڽ�� ������ �ɷ������� �����ϱ� ����
�����ͺ��̽��� �ʿ��ϴٰ� ���. �� �����͵� �ֹ�, ��, ���� ���� �
�ϰ����ִ� ���� ���õ� ������ �����͵�� �� �پ��ϰ� ������ ��̺��
�̷� ��̴�. ��̺� ������ �Ŀ���, �پ��� ���� ��� �����ͺ��̽����� ����
���� ���Ǵ� join�� ���۹��� ���� �����ϰڴ�.
ù��° ��̺� �������ڰ� �з��� �پ��� Ÿ��� PC��� �����ͷ� ������ ��̴�.
----------------------------------------------
mysql> create table pcs (
-> pid INT, // product id
-> spec char(3),
-> os char(10),
-> ram INT,
-> hd char(4)
-> );
-----------------------------------------------
�ι�° ��̺� ��������� �پ��� ���鿡 ���� �����ͷ� �̷�� ��̴�.
-----------------------------------------------
mysql> create table clients (
-> name char(25),
-> cid char(8), // client id
-> email char(25),
-> tel char(10)
-> );
-----------------------------------------------
����° ��̺� �ֹ� ������ ���� ����Ÿ�� ����� ��̴�.
-----------------------------------------------
mysql> create table orders (
-> order_date date,
-> pid INT,
-> cid char(8)
-> );
-----------------------------------------------
�ߡ� �ڷ�(Data) ����ϱ�
������ ��̺� �Ʒ��� ���� �ڷḦ ����� ����.
�� pcs (��̺�1)
+------+------+-------+------+------+
| pid | spec | os | ram | hd |
+------+------+-------+------+------+
| 1 | 386 | Linux | 64 | 3.1 |
| 2 | 386 | Linux | 128 | 4.2 |
| 3 | 486 | WinNT | 64 | 3.1 |
| 4 | 586 | Linux | 128 | 4.2 |
| 5 | 586 | Win98 | 128 | 6.4 |
+------+------+-------+------+------+
[��Թ�]
-----------------------------------------------------------
mysql> INSERT INTO pcs (pid, spec, os, ram, hd)
-> VALUES (1, '386', 'Linux', 64, '3.1');
-----------------------------------------------------------
�� clients (��̺�2)
+--------+---------+---------------------------+------------+
| name | cid | email | tel |
+--------+---------+---------------------------+------------+
| ������ | wjh-042 | haremoon@won.hongik.ac.kr | 123-456-78 |
| ȫ�浿 | hgd-043 | honggd@won.hongik.ac.kr | 421-555-34 |
| �̻ڴ� | pty-042 | pretty@won.hongik.ac.kr | 459-555-32 |
| ������ | ugy-043 | ugly@won.hongik.ac.kr | 439-555-88 |
+--------+---------+---------------------------+------------+
[��Թ�]
-----------------------------------------------------------
mysql> INSERT INTO clients (name, cid, email, tel)
-> VALUES ('������', 'wjh-042',
-> 'haremoon@won.hongik.ac.kr', '123-456-7890');
-----------------------------------------------------------
�� orders (��̺�3)
+------------+------+---------+
| order_date | pid | cid |
+------------+------+---------+
| 1999-12-05 | 2 | wjh-042 |
| 1999-12-04 | 3 | hgd-043 |
| 1999-12-04 | 1 | wjh-042 |
| 1999-12-05 | 2 | wjh-042 |
| 1999-12-12 | 5 | ugy-043 |
| 1999-12-05 | 5 | pty-042 |
+------------+------+---------+
[��Թ�]
-----------------------------------------------------------
mysql> INSERT INTO orders (order_date, pid, cid)
-> VALUES ('1999-12-05', 2, 'wjh-042');
-----------------------------------------------------------
��, ������� ����� 3���� ��̺�κ��� �ʿ��� �������� ������ �����ϴ�
���� ���� �˾ƺ���. ���� ���� ��簡 Ư���� ���� Ư���� PC�� �ֹ��� ���
���� �̸�� ���� ���Ѵٰ� ���! �Ǵ� Ư���� ���� ���� �ۼ��� �ֹ�����
�ִ� RAM�� �� ����ޱ⸦ ���Ѵٰ� ���. �̷��� ��� �ϵ� �پ��� join����
���� ���� ����� �� �ִ�. ����� ��̺� ����� ù��° join���� �����غ���
�ۼ��غ���.
�ߡ� The Cross Join
Cross Join�� ���� �⺻���� join�� Ÿ����� �� ��̺� �ִ� ������ ���� �ٸ�
��̺�� ��� ���� �����ϰ� ��ġ�Ǿ� ��µȴ�. �ɷ�������� �����, ��� join
�� ����� Ư¡�� ��Ÿ���ش�.
Cross Join�� ������ �� :
----------------------------------------------
mysql> SELECT * FROM pcs, clients;
----------------------------------------------
�ſ� ���� ����� ��µ� ��̴�. ��̺�1(pcs)�� �ִ� ������ ���� ��̺�2(clients)
�� ��� ���� ��ġ�ȴٴ� ��� ������. ����, 3 ���� ���� ��̺�1(pcs)�� 4 ����
���� ��̺�2(clients)�� ����� join���� �� 12 ���� ��̺� �����̴�.
�� cross-join�� ��̺�1�� �ִ� ������ ����� ��̺�2�� �ִ� ��翭�� �� ����
����� ����Ѵٰ� ����ϴ� ��� ���� �Ͱ���.
ù��° join�� ���������� �����ߴٸ� ����� ���� ���� ��� ��̴�.
�Ʒ��� ���� ������ ���� � ���� ��µ�� �����غ���.
-----------------------------------------------------------------------------
mysql> select c.name, o.cid from orders o, clients c where o.cid = "wjh-042";
-----------------------------------------------------------------------------
+--------+---------+
| name | cid |
+--------+---------+
| ������ | wjh-042 |
| ������ | wjh-042 |
| ������ | wjh-042 |
| ȫ�浿 | wjh-042 |
| ȫ�浿 | wjh-042 |
| ȫ�浿 | wjh-042 |
| �̻ڴ� | wjh-042 |
| �̻ڴ� | wjh-042 |
| �̻ڴ� | wjh-042 |
| ������ | wjh-042 |
| ������ | wjh-042 |
| ������ | wjh-042 |
+--------+---------+
������ ��� ���� ��µǾ���? clients ��̺� �ִ� ������ name�� orders���
��� "wjh-042"�� ����� ������ ��ġ�Ǿ� ��µǾ���. ���ݱ��� ����� Cross
Join�� �����ϴµ� ������ ����Ƿ� �ٸ� ���Ǹ� ����ذ��� JOIN�� Ȱ���غ���
�ٶ���.
NOTE : �� ��̺�� �̸��� ���� �־� ����ұ�? Aliases(����)�� ���Ǹ� �Է���
�� �ݺ����� Ű�� �Է�� �ٿ��ִ� ������ ���Ǿ���. ���� ���� ������ ��
�� �ݺ������� 'clients'�� ��� ��� �Է��ϴ� ��ſ�, ���dz��� 'from clients c'
�� ������ְ� 'c'�� ����� �� �ִ�.
Cross Join�� ��̺�� ������ �ֱ�� �����, �ɷ�������� ���ϴ�. ����
������ ��̺��� ���� �츮�� ���ϴ� ����Ÿ�� ��� �ϸ� ���� ������ �� �ִ��
��� ��� �� �о�� �ٶ���.
�ߡ� The Equi-join
Equi-join�� �� ��̺� �ִ� ��� ���� �ι�°(�Ǵ� �ټ���) ��̺��� ��Ե�
���� ��ġ �� �� ����ȴ�.
product id �� 1�� PC�� �ֹ��� ���� ���� ���Ѵٰ� ������ ����.
-------------------------------------------------------------------
mysql> select p.os, c.name from orders o, pcs p, clients c
-> where p.pid=o.pid and o.pid = 1 and o.cid=c.cid;
-------------------------------------------------------------------
+-------+--------+
| os | name |
+-------+--------+
| Linux | ������ |
+-------+--------+
�� Non-Equi-join
Equi-join�� �ټ��� ��̺�� ��̿��� ��ġ�ϴ� �ڷ�鸸�� �����س���. ����
���� ��ġ��� ��� �ڷ�鸸�� �����ؾ� �Ѵٸ�...? ���� ���, ���� ��簡
�ֹ��� pid�� ��ǰ�� pid���� �� ū order id�� ��� �ü��(OS)�� ����
�ʿ�� �Ѵٸ� ��� �� ��ΰ�? ����� �̸��� non-equi join��� �ϰڴ�.
-------------------------------------------------------------------
mysql> SELECT p.os, o.pid from orders o, pcs p where o.pid > p.pid;
-------------------------------------------------------------------
+-------+------+
| os | pid |
+-------+------+
| Linux | 2 |
| Linux | 3 |
| Linux | 2 |
| Linux | 5 |
| Linux | 5 |
| Linux | 3 |
| Linux | 5 |
| Linux | 5 |
| WinNT | 5 |
| WinNT | 5 |
| Linux | 5 |
| Linux | 5 |
+-------+------+
orders ��̺�� pid�� pcs��̺�� pid���� �� ū ��� ����� ��ġ�� ��̴�.
��DZ�� ���캸��, �������� ����� �� ������ cross-join ��� �ľ��� �� ��
�� ��̴�. ��翡�Դ� Ư���ϰ� ������� ��� ���� �����, �ſ� ������ ��
��� left join�� ���� �غ� �������� �������. ��, ��� ������� ����
left join�� ����� �� ������ ���ִ� �ɼǵ鿡 ���� ��������� �˾ƺ���.
�ߡ� The Left Join
Left Join�� ���ڰ� ��� ����� ������ ����ִ� ��� ������ �ڷḦ ����
�ϰ��Ѵ�. ��̺� join�� ���� ������ �ɼ����, ��̺� �ſ� ���� ������ ��
�ְ� �Ѵ�.
���� ��簡 ���� �ڼ��, �ڼ��, �ڼ��ϰ�, �ڼ��ϰ�!�� ��ģ�ٰ� �����غ���.
left join�� �츮�� ������ �ذ��� �� ��̴�.
-------------------------------------------------------------------
mysql> select * from orders left join pcs on orders.pid = pcs.pid;
-------------------------------------------------------------------
+------------+------+---------+------+------+-------+------+------+
| order_date | pid | cid | pid | spec | os | ram | hd |
+------------+------+---------+------+------+-------+------+------+
| 1999-12-05 | 2 | wjh-042 | 2 | 386 | Linux | 128 | 4.2 |
| 1999-12-04 | 3 | hgd-043 | 3 | 486 | WinNT | 64 | 3.1 |
| 1999-12-04 | 1 | wjh-042 | 1 | 386 | Linux | 64 | 3.1 |
| 1999-12-05 | 2 | wjh-042 | 2 | 386 | Linux | 128 | 4.2 |
| 1999-12-12 | 5 | ugy-043 | 5 | 586 | Win98 | 128 | 6.4 |
| 1999-12-05 | 5 | pty-042 | 5 | 586 | Win98 | 128 | 6.4 |
+------------+------+---------+------+------+-------+------+------+
���� �ֹ��� ��� PC��� ���� ������ ����. ���� ���, PHP3 �Ǵ� Perl
��ũ��Ʈ�� ����� �������� ����ϴµ� ����� ���� �ִ�. ���鿡�� �츮ȸ
��κ��� ������ ��� ��ǰ�� ���� ������ ��Ϸ� ������ �� ������
clients ��̺� ������ ����� �� ��� ��̴�.
�Ʒ��� ������ �츮�� ��ǰ�� id(pid)�� 3�� PC�� �������� �� �� �ִ�.
-------------------------------------------------------------------
mysql> select * from orders left join pcs on pcs.pid=3 and pcs.pid=orders.pid;
-------------------------------------------------------------------
+------------+------+---------+------+------+-------+------+------+
| order_date | pid | cid | pid | spec | os | ram | hd |
+------------+------+---------+------+------+-------+------+------+
| 1999-12-05 | 2 | wjh-042 | NULL | NULL | NULL | NULL | NULL |
| 1999-12-04 | 3 | hgd-043 | 3 | 486 | WinNT | 64 | 3.1 |
| 1999-12-04 | 1 | wjh-042 | NULL | NULL | NULL | NULL | NULL |
| 1999-12-05 | 2 | wjh-042 | NULL | NULL | NULL | NULL | NULL |
| 1999-12-12 | 5 | ugy-043 | NULL | NULL | NULL | NULL | NULL |
| 1999-12-05 | 5 | pty-042 | NULL | NULL | NULL | NULL | NULL |
+------------+------+---------+------+------+-------+------+------+
�� The Using Clause
left join�� �ణ�� �ɼ�� �־� �� �̻�� ��̺� �ִ� ������ �÷��� ���� ��
��� ������������ �ִ�. on�� using�ɼ�� ���Ǹ�, �Ʒ��� ������ ������.
--------------------------------------------------------------------
## ������ ������ �Ʒ��� ������, ������ ���� ��ڰ� MySQL�Ŵ��� �����
������� ������ �ߴ�.##
mysql> SELECT * from clients join on orders where clients.cid = orders.cid;
mysql> SELECT * from clients join on orders using (cid);
-------------------------------------------------------------------
==> ������ ����
-------------------------------------------------------------------
mysql> SELECT * from clients left join orders on clients.cid = orders.cid;
-------------------------------------------------------------------
�Ǵ� �Ʒ��� ���� ��Ÿ�� ���� �ִ�.
-------------------------------------------------------------------
mysql> SELECT * from clients left join orders using (cid);
-------------------------------------------------------------------
�� ���� ��� �Ȱ��� ���� ��µ� ��̴�.
+--------+---------+---------------------------+------------+------------+------+---------+
| name | cid | email | tel | order_date | pid | cid |
+--------+---------+---------------------------+------------+------------+------+---------+
| ������ | wjh-042 | haremoon@won.hongik.ac.kr | 123-456-78 | 1999-12-05 | 2 | wjh-042 |
| ������ | wjh-042 | haremoon@won.hongik.ac.kr | 123-456-78 | 1999-12-04 | 1 | wjh-042 |
| ������ | wjh-042 | haremoon@won.hongik.ac.kr | 123-456-78 | 1999-12-05 | 2 | wjh-042 |
| ȫ�浿 | hgd-043 | honggd@won.hongik.ac.kr | 421-555-34 | 1999-12-04 | 3 | hgd-043 |
| �̻ڴ� | pty-042 | pretty@won.hongik.ac.kr | 459-555-32 | 1999-12-05 | 5 | pty-042 |
| ������ | ugy-043 | ugly@won.hongik.ac.kr | 439-555-88 | 1999-12-12 | 5 | ugy-043 |
+--------+---------+---------------------------+------------+------------+------+---------+
�ٸ� ������ ���� ������ ���� left join�� ���� �����ϱ� �ٶ���. ���θ� �ϴٺ���
left join�� ������� ����Ȱ���� �ſ� �߿��� ����� �� �ٴ� ��� ���� ��̴�.
��̺� join�� ���� ������ ���� ��� ��ȯ�ϰ� �ʹٸ� http://www.mysql.com�� �ִ�
�پ��� ��� �� üũ�� ���� �ٶ���.
�ߡ� Self-joins
Self-join�� �����ڰ� �ϳ��� ��̺� ���õ� ����Ÿ�� ���߽�Ű�� ������ ����
������ �ش�. ���, self-join�� �� �ڽ�� ��̺� �����ϴ� �Ϳ� ���� ����ȴ�.
����� ���ظ� ���� ���� ��� �����ϰڴ�.
��ǻ�� ��ũ����̼�� ����µ� ���Ǵ� �ϵ���� �پ��� ��ǰ�� ���� ������
���� �ſ� ū ����Ÿ���̽��� �����ؾ� �Ѵٰ� �������. ��ũ����̼�� ����ũ,
PC, �����, Ű����, ���콺���� �̷�� �ִ�. �Դٰ�, ����ũ�� ��ũ����̼��
��� �ٸ� �κ�� '�θ�'��� ������ �� �ִ�. �츮�� �� ��ũ����̼�� ���ڵ尡
��Ȯ�� �ڷ�� �����DZ⸦ ���� ��̸�, ������ ID��ȣ�� �ο�����ν� ��ũ����̼�
�� ��� �κ�� ��ü������ ���ý�ų ��̴�. ���, �� �κ�� �� �и��ϰ� ��ִ�
������ ID��ȣ�� �װ�� �θ�(����ũ) ID��ȣ�� Ȯ���ϱ� ����, �ΰ��� ID ��ȣ��
��� ��̴�.
��̺�� �Ʒ��� ���ٰ� �������.
mysql> select * from ws;
+---------+-----------+-----------+
| uniq_id | name | parent_id |
+---------+-----------+-----------+
| d001 | desktop | NULL |
| m4gg | monitor | d001 |
| k235 | keyboar | d001 |
| pc345 | 200mhz pc | d001 |
| d002 | desktop | NULL |
| m156 | monitor | d002 |
| k9334 | keyboar | d002 |
| pa556 | 350mhz pc | d002 |
+---------+-----------+-----------+
desktop�� �� ���õ� ��� �κе�� �θ�� �����Ƿ� parent_id�� ������ ���
����� �ָ����. ���ݺ��� ������ ������ ���� ���Ǹ� ����� ��̴�. self-join
�� ����� ���� �����ϱ� ���� ��̺� �����ϰ� ������.
-------------------------------------------------------------------
mysql> select t1.*, t2.* from ws as t1, ws as t2;
-------------------------------------------------------------------
��� ��µǴ°�? ���ó��, ù��° ��̺�� �� ����� �ι�° ��̺� �ִ�
��� ���鿡 ��ġ�Ǿ� ����� ��µ� ��̴�. �츮���� �ſ� ��������� �����
�ٽ� �ѹ� �õ��غ��� Ȯ���� ����ٶ���. ���� ���ִ� ���� ���ڴ�.
-------------------------------------------------------------------
mysql> select parent.uniq_id, parent.name, child.uniq_id, child.name
-> from ws as parent, ws as child
-> where child.parent_id = parent.uniq_id and parent.uniq_id = "d001";
-------------------------------------------------------------------
��̷ο� ���� ��µ� ��̴�.
+---------+---------+---------+-----------+
| uniq_id | name | uniq_id | name |
+---------+---------+---------+-----------+
| d001 | desktop | m4gg | monitor |
| d001 | desktop | k235 | keyboar |
| d001 | desktop | pc345 | 200mhz pc |
+---------+---------+---------+-----------+
self-join�� ��̺�� �ڷḦ ����ϴ� �����ε� ���ȴ�. ��̺��� �ִ�
uniq_id�÷��� ��̺��� �����ؾ� �ϸ�, ���� ����Ÿ�� ��Ʈ���� ��� �����
�ʰ� ���� uniq_id�� ���� �ΰ��� ��� ����Ÿ���̽��� �Էµȴٸ� ���� ���
���� �����̴�. �̷� ��� �������� self-join�� ����� üũ�� �� �ִ�.
�츮�� 350mhz pc�� uniq_id�� 'm156'(�� ���� ��ũ����̼� 'd002'�� ����
������ uniq_id ���̴�.)�� �ǵ��� �����ߴٰ� �������.
��̺�� ���� ����� ���� �����Ѵ�.
-------------------------------------------------------------------
mysql> update ws set uniq_id = 'm156' where name = '350mhz pc';
-------------------------------------------------------------------
�Ʒ��� ���� ����� ws��̺� self-join�� �����غ���.
-------------------------------------------------------------------
mysql> select parent.uniq_id, parent.name, child.uniq_id, child.name
-> from ws as parent, ws as child
-> where parent.uniq_id = child.uniq_id and parent.name <> child.name;
-------------------------------------------------------------------
�Ʒ��� ���� ��µ� ��̴�.
+---------+-----------+---------+-----------+
| uniq_id | name | uniq_id | name |
+---------+-----------+---------+-----------+
| m156 | 350mhz pc | m156 | monitor |
| m156 | monitor | m156 | 350mhz pc |
+---------+-----------+---------+-----------+
Table join�� �����ͺ���� ������ ���� ����ٰ�̴�. ������ ���ظ� ��Ȯ�ϰ�
�ϱ��ؼ� �������� ��� ���� �پ��ϰ� ��ȭ���� �����غ��� �ٶ���.