1 / 15

09 06/23 PHP と SQL (MySQL) の連携 その 3

平成 20 年度 情報科学 III (理系コア科目・2年生). 09 06/23 PHP と SQL (MySQL) の連携 その 3. 担当 岡村耕二 月曜日 2限. http://okaweb.ec.kyushu-u.ac.jp/lectures/jk3/. 本資料の一部は、堀良彰准教授、天野浩文准教授等による 以前の講義資料をもとにしています。. 演習問題. テーブルのカラムとして 商品名 価格 色 重さ   を、用意し、「商品名」で検索し、「色」「重さ」はオプションで表示できるようなプログラムを作成せよ。 テーブルのカラムとして テーブル1

gur
Download Presentation

09 06/23 PHP と SQL (MySQL) の連携 その 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 平成20年度 情報科学III (理系コア科目・2年生) 09 06/23 PHP と SQL (MySQL) の連携 その3 担当 岡村耕二 月曜日 2限 http://okaweb.ec.kyushu-u.ac.jp/lectures/jk3/ 本資料の一部は、堀良彰准教授、天野浩文准教授等による 以前の講義資料をもとにしています。

  2. 演習問題 • テーブルのカラムとして • 商品名 • 価格 • 色 • 重さ   を、用意し、「商品名」で検索し、「色」「重さ」はオプションで表示できるようなプログラムを作成せよ。 • テーブルのカラムとして • テーブル1 • 商品名 • 価格 • テーブル2 • 商品名 • 色 • テーブル3 • 商品名 • 重さ を用意して、「商品名」で検索して、「価格」「色」「重さ」が表示されるプログラムを作成せよ。

  3. テーブルの作成 mysql> create table ex0630_1 (name char(32), price char(32)); mysql> create table ex0630_2 (name char(32), color char(32)); mysql> create table ex0630_3 (name char(32), weight char(32));

  4. データ投入 mysql> insert into ex0630_1 set name="book", price="1000"; mysql> insert into ex0630_1 set name="pen", price="100"; mysql> insert into ex0630_1 set name="cd", price="3000"; mysql> insert into ex0630_2 set name="book", color="black"; mysql> insert into ex0630_2 set name="pen", color="red"; mysql> insert into ex0630_2 set name="cd", color="silver"; mysql> insert into ex0630_3 set name="book", weight="100"; mysql> insert into ex0630_3 set name="pen", weight="10"; mysql> insert into ex0630_3 set name="cd", weight="5";

  5. 検索例 mysql> select * from ex0630_1 join ex0630_2, ex0630_3; +------+-------+------+--------+------+--------+ | name | price | name | color | name | weight | +------+-------+------+--------+------+--------+ | book | 1000 | book | black | book | 100 | | pen | 100 | book | black | book | 100 | | cd | 3000 | book | black | book | 100 | | book | 1000 | pen | red | book | 100 | | pen | 100 | pen | red | book | 100 | | cd | 3000 | pen | red | book | 100 | | book | 1000 | cd | silver | book | 100 | | pen | 100 | cd | silver | book | 100 | | cd | 3000 | cd | silver | book | 100 | | book | 1000 | book | black | pen | 10 | | pen | 100 | book | black | pen | 10 | | cd | 3000 | book | black | pen | 10 | | book | 1000 | pen | red | pen | 10 | | pen | 100 | pen | red | pen | 10 | | cd | 3000 | pen | red | pen | 10 | | book | 1000 | cd | silver | pen | 10 | | pen | 100 | cd | silver | pen | 10 | | cd | 3000 | cd | silver | pen | 10 | | book | 1000 | book | black | cd | 5 | | pen | 100 | book | black | cd | 5 | | cd | 3000 | book | black | cd | 5 | | book | 1000 | pen | red | cd | 5 | | pen | 100 | pen | red | cd | 5 | | cd | 3000 | pen | red | cd | 5 | | book | 1000 | cd | silver | cd | 5 | | pen | 100 | cd | silver | cd | 5 | | cd | 3000 | cd | silver | cd | 5 | +------+-------+------+--------+------+--------+

  6. 検索例 mysql> select * from ex0630_1 join ex0630_2, ex0630_3 on ex0630_1.name=ex0630_2.name and ex0630_1.name=ex0630_3.name; +------+-------+------+--------+------+--------+ | name | price | name | color | name | weight | +------+-------+------+--------+------+--------+ | book | 1000 | book | black | book | 100 | | pen | 100 | pen | red | pen | 10 | | cd | 3000 | cd | silver | cd | 5 | +------+-------+------+--------+------+--------+

  7. 検索例 mysql> select ex0630_1.name,ex0630_1.price,ex0630_2.color,ex0630_3.weight from ex0630_1 join ex0630_2, ex0630_3 on ex0630_1.name=ex0630_2.name and ex0630_1.name=ex0630_3.name; +------+-------+--------+--------+ | name | price | color | weight | +------+-------+--------+--------+ | book | 1000 | black | 100 | | pen | 100 | red | 10 | | cd | 3000 | silver | 5 | +------+-------+--------+--------+

  8. 検索例 mysql> select ex0630_1.name,ex0630_1.price,ex0630_2.color,ex0630_3.weight from ex0630_1 join ex0630_2, ex0630_3 on ex0630_1.name=ex0630_2.name and ex0630_1.name=ex0630_3.name where ex0630_1.name="pen"; +------+-------+-------+--------+ | name | price | color | weight | +------+-------+-------+--------+ | pen | 100 | red | 10 | +------+-------+-------+--------+

  9. データベースで数字を扱って見る。 mysql> create table ex0707_1 (name char(32), price int); mysql> create table ex0707_2 (name char(32), color char(32)); mysql> create table ex0707_3 (name char(32), weight int); mysql> insert into ex0707_1 set name="book", price=1000; mysql> insert into ex0707_1 set name="pen", price=100; mysql> insert into ex0707_1 set name="cd", price=3000;

  10. 検索例 mysql> select ex0707_1.name,ex0707_1.price,ex0707_2.color,ex0707_3.weight from ex0707_1 join ex0707_2, ex0707_3 on ex0707_1.name=ex0707_2.name and ex0707_1.name=ex 0707_3.name; +------+-------+--------+--------+ | name | price | color | weight | +------+-------+--------+--------+ | book | 1000 | black | 100 | | pen | 100 | red | 10 | | cd | 3000 | silver | 5 | +------+-------+--------+--------+

  11. 検索例: 価格が2000以下の商品を検索 mysql> select ex0707_1.name,ex0707_1.price,ex0707_2.color,ex0707_3.weight from ex0707_1 join ex0707_2, ex0707_3 on ex0707_1.name=ex0707_2.name and ex0707_1.name=ex0707_3.name where ex0707_1.price <= 2000; +------+-------+-------+--------+ | name | price | color | weight | +------+-------+-------+--------+ | book | 1000 | black | 100 | | pen | 100 | red | 10 | +------+-------+-------+--------+

  12. 検索例: 価格が2000以下で、重さが50以上のものを検索 mysql> select ex0707_1.name,ex0707_1.price,ex0707_2.color,ex0707_3.weight from ex0707_1 join ex0707_2, ex0707_3 on ex0707_1.name=ex0707_2.name and ex0707_1.name=ex0707_3.name where ex0707_1.price <= 2000 and ex0707_3.weight >= 50; +------+-------+-------+--------+ | name | price | color | weight | +------+-------+-------+--------+ | book | 1000 | black | 100 | +------+-------+-------+--------+

  13. 練習問題 • 商品、価格、販売店、販売店距離のデータがあるとき • 価格で商品を選択できるプログラムを作成せよ • 例:100円以下とか • 販売店の距離で選択できるプログラムを作成せよ • 例:500m以内とか • 価格と販売店の距離両方の条件を選択できるプログラムを作成せよ。 • 例:200円以下で距離が1000m以内 • 注意 • 普通、全部の販売店で同じ商品があるわけではないと思いますので、そのあたり注意して設計してください。 • まず、SQL で動作を確認すること

  14. SQLコマンドの例 > create table ex0707b_1 (name char(32),price int); > create table ex0707b_2 (name char(32),shop char(32)); > create table ex0707b_3 (shop char(32),distance int); > select ex0707b_1.name, ex0707b_1.price, ex0707b_2.shop, ex0707b_3.distance from ex0707b_1 join ex0707b_2, ex0707b_3 on ex0707b_1.name = ex0707b_2.name and ex0707b_2.shop = ex0707b_3.shop > select ex0707b_1.name, ex0707b_1.price, ex0707b_2.shop, ex0707b_3.distance from ex0707b_1 join ex0707b_2, ex0707b_3 on ex0707b_1.name = ex0707b_2.name and ex0707b_2.shop = ex0707b_3.shop where ex0707b_1.price < 500 and ex0707b_3.distance < 100;

  15. 演習問題 • 最終課題を各人考えてきてください。 • 課題の仕様の条件 • データーベースのテーブルを2つ以上使用して、組み合わて(join を使って)検索ができること。 • いままで、講義で出なかったような内容を考えてみてください。(名簿系、価格系以外) • 7/14(最終講義)では、各人、講義で課題の説明(プログラムはできている必要はない。もちろん、できていてもいい。)をしてもらいます。 • データベースの設計 • Web での見え方、PHP の設計 • 不足分は必要に応じて修正してもらい、最終課題に取り組んでもらいます。

More Related