前のページ < | > 次のページ

(8) 複数テーブルからレコードを取り出す

  • 下記にサンプルデータを記す。これを用いて集計等を行った。
+ ←クリックで開く
create table sales (
id int auto_increment,
date datetime not null,
customer varchar(50) not null,
price int not null,
count int not null,
goods_id int not null,
primary key (id)
);
create table goods (
goods_id int auto_increment,
goods_name varchar(50) not null,
primary key (goods_id)
);
insert into sales values (1, '2007/01/01', '山田産業(株)', 50, 12, 1);
insert into sales values (2, '2007/01/12', '(株)田中商事', 100, 10, 2);
insert into sales values (3, '2007/01/15', '鈴木建設(株)', 50, 24, 1);
insert into sales values (4, '2007/01/25', '山田産業(株)', 150, 10, 3);
insert into sales values (5, '2007/02/03', '鈴木建設(株)', 100, 20, 2);
insert into sales values (6, '2007/02/09', '(株)田中商事', 100, 10, 2);
insert into sales values (7, '2007/02/14', '鈴木建設(株)', 50, 24, 1);
insert into sales values (8, '2007/02/22', '(株)田中商事', 50, 12, 1);
insert into sales values (9, '2007/03/05', '鈴木建設(株)', 150, 15, 3);
insert into sales values (10, '2007/03/12', '山田産業(株)', 150, 10, 3);
insert into goods values (1, '鉛筆');
insert into goods values (2, '消しゴム');
insert into goods values (3, 'ノート');

(1)table: sales
id date customer price count goods_id
1 2007/01/01 山田産業(株) 50 12 1
2 2007/01/12 (株)田中商事 100 10 2
3 2007/01/15 鈴木建設(株) 50 24 1
4 2007/01/25 山田産業(株) 150 10 3
5 2007/02/03 鈴木建設(株) 100 20 2
6 2007/02/09 (株)田中商事 100 10 2
7 2007/02/14 鈴木建設(株) 50 24 1
8 2007/02/22 (株)田中商事 50 12 1
9 2007/03/05 鈴木建設(株) 150 15 3
10 2007/03/12 山田産業(株) 150 10 3
(2)table: goods
goods_id goods_name
1 鉛筆
2 消しゴム
3 ノート


  • 複数テーブルのフィールドを結合させる
select フィールド名リスト from テーブル名リスト where 結合の条件と絞込みの条件 
order by 並べ替えに使うフィールド 

  • フィールド名リスト
    • salesテーブルのnumberフィールドを取り出す場合:sales.number

  • 結合の条件 例
    • goods_idフィールドで2つのテーブルを結合し、salesテーブルのcustomerフィールドの値が「山田産業(株)」になっているレコードだけ取り出す。
sales.goods_id = goods.goods_id and sales.customer = '山田産業(株)'

  • テーブル名リスト
    • salesテーブルとgoodsテーブルを結合する場合、テーブル名リストは「sales, goods」と書く。

  • テーブル名に別名を割り当てる
    • フィールド名の前にテーブル名をつけるとSQLの文が長くなりがち。それぞれのテーブル名を「テーブル名 別名」とすることで、短い別名を割り当てることができる。
    • salesテーブルとgoodsテーブルを結合する場合、テーブルリストの部分を「sales s, goods g」のように書くと、sales/goodsテーブルにそれぞれ「s」「g」の別名が付く。

  • salesテーブルのレコードをすべて取り出す。
    • salesテーブルとgoodsテーブルを結合して、全て取り出す。
select s.id, s.date, s.customer, s.price, s.count, g.goods_name 
from sales s, goods g 
where s.goods_id = g.goods_id
  • 結果


  • 特定の注文だけ取り出す
    • 2テーブルを結合し、customerフィールドの値が「山田産業(株)」のフィールドだけ取り出す。
select s.id, s.date, s.customer, s.price, s.count, g.goods_name 
from sales s, goods g 
where s.goods_id = g.goods_id and s.customer


  • 得意先/商品別の注文状況を出力する。
select s.customer, sum(s.price *s.count), g.goods_name, count(g.goods_name) 
from sales s, goods g 
where s.goods_id = g.goods_id 
group by s.customer, g.goods_name


MYSQLその他の関数

  • MySQLサーバーのバージョンを表示
select version();

  • 現在使っているデータベースを表示
select database();

  • 現在のユーザを表示
select user();

  • 引数で指定した文字の文字コードを表示
select charset('この文字');


※ XAMPP 1.7.7 [PHP: 5.3.8] での設定項目です。






前のページ < | > 次のページ
◆ ◆ ◆

最終更新:2012年01月31日 16:00
添付ファイル