「php05」の編集履歴(バックアップ)一覧はこちら
「php05」(2012/01/31 (火) 16:00:31) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
[[前のページ>php04]] < | > [[次のページ>php08]]
*(8) 複数テーブルからレコードを取り出す
・下記にサンプルデータを記す。これを用いて集計等を行った。
#region(close,←クリックで開く)
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, 'ノート');
#endregion
(1)table: sales
|BGCOLOR(#e4ccff):&bold(){id}|BGCOLOR(#e4ccff):&bold(){date}|BGCOLOR(#e4ccff):&bold(){customer}|BGCOLOR(#e4ccff):&bold(){price}|BGCOLOR(#e4ccff):&bold(){count}|BGCOLOR(#e4ccff):&bold(){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
|BGCOLOR(#e4ccff):&bold(){goods_id}|BGCOLOR(#e4ccff):&bold(){goods_name}|
|1|鉛筆|
|2|消しゴム|
|3|ノート|
・&bold(){複数テーブルのフィールドを結合させる}。
> 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」と書く。
・&bold(){テーブル名に別名を割り当てる}
・・フィールド名の前にテーブル名をつけるとSQLの文が長くなりがち。それぞれのテーブル名を「テーブル名 別名」とすることで、短い別名を割り当てることができる。
・・salesテーブルとgoodsテーブルを結合する場合、テーブルリストの部分を「sales s, goods g」のように書くと、sales/goodsテーブルにそれぞれ「s」「g」の別名が付く。
・&bold(){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
・結果
#image(ketugou.gif,center)
・&bold(){特定の注文だけ取り出す}
・・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
・&bold(){得意先/商品別の注文状況を出力する。}
> 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] での設定項目です。
----
#center(){[[前のページ>php03]] < | > [[次のページ>php08]]}
#center(){◆ ◆ ◆}
----
[[前のページ>php04]] < | > [[次のページ>php08]]
*(8) 複数テーブルからレコードを取り出す
・下記にサンプルデータを記す。これを用いて集計等を行った。
#region(close,←クリックで開く)
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, 'ノート');
#endregion
(1)table: sales
|BGCOLOR(#e4ccff):&bold(){id}|BGCOLOR(#e4ccff):&bold(){date}|BGCOLOR(#e4ccff):&bold(){customer}|BGCOLOR(#e4ccff):&bold(){price}|BGCOLOR(#e4ccff):&bold(){count}|BGCOLOR(#e4ccff):&bold(){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
|BGCOLOR(#e4ccff):&bold(){goods_id}|BGCOLOR(#e4ccff):&bold(){goods_name}|
|1|鉛筆|
|2|消しゴム|
|3|ノート|
・&bold(){複数テーブルのフィールドを結合させる}。
> 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」と書く。
・&bold(){テーブル名に別名を割り当てる}
・・フィールド名の前にテーブル名をつけるとSQLの文が長くなりがち。それぞれのテーブル名を「テーブル名 別名」とすることで、短い別名を割り当てることができる。
・・salesテーブルとgoodsテーブルを結合する場合、テーブルリストの部分を「sales s, goods g」のように書くと、sales/goodsテーブルにそれぞれ「s」「g」の別名が付く。
・&bold(){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
・結果
#image(ketugou.gif,center)
・&bold(){特定の注文だけ取り出す}
・・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
・&bold(){得意先/商品別の注文状況を出力する。}
> 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] での設定項目です。
----
#center(){[[前のページ>php04]] < | > [[次のページ>php08]]}
#center(){◆ ◆ ◆}
----