/* $numが1か?? */
$num = ($i == 1) ? '$iは1です':'$iは1以外です';
2012年9月5日水曜日
2012年8月3日金曜日
ポストグレ PostGres ダンプ色々
//***********インサート形式でデータを抽出***********//
pg_dump -d DB名 -t テーブル名
//***********DBをバックアップ***********//
pg_dump DB名 > ファイル名.out
2012年7月13日金曜日
IEでJPGをアップロードするときの注意
jpgの場合は、IEでは$_FILES['upload_image']['type'] == 'image/pjpeg'となるので注意
<!---------------------phpソース--------------------->
<?php
session_cache_limiter('none');
session_start();
require_once('./helper/method.php');
if($_FILES['upload_image']['name']){
$msg = "OK";
$upload_dir = '../file/plan/pc/';
$upload_dir_sp = '../file/plan/sp/';
$filename = $_FILES['upload_image']['name'];
$uptime = date("YmdH_i_s");
$name = $_FILES['upload_image']['tmp_name'];
$flag= "";
//画像アップ処理
if($_FILES['upload_image']['type'] == 'image/jpeg' || $_FILES['upload_image']['type'] == 'image/pjpeg'){
$testmsg = '.jpg';
$upname = $upload_dir.$uptime.'.jpg';
$upname_sp = $upload_dir_sp.$uptime.'.jpg';
$judg = move_uploaded_file($_FILES['upload_image']['tmp_name'],$upname);
m_cpy_img($upname,$upname_sp);
}else
if($_FILES['upload_image']['type'] == 'image/gif'){
$upname = $upload_dir.$uptime.'.gif';
$upname_sp = $upload_dir_sp.$uptime.'.gif';
$judg = move_uploaded_file($_FILES['upload_image']['tmp_name'],$upname);
m_cpy_img($upname,$upname_sp);
}else
if($_FILES['upload_image']['type'] == 'image/ping'){
$upname = $upload_dir.$uptime.'.png';
$upname_sp = $upload_dir_sp.$uptime.'.png';
$judg = move_uploaded_file($_FILES['upload_image']['tmp_name'],$upname);
m_cpy_img($upname,$upname_sp);
}else{
$judg = false;
}
if($judg){
$msg = "OK";
smart_resize_image($upname,600,600,true);
smart_resize_image($upname_sp,300,200,true);
$dir = '/file/plan/pc/';
$_SESSION['input_data']['pl_img1'] = $dir.basename($upname);
}
}else{
$msg = "画像が選択されていません";
}
//************************************************
// ファイルコピー
//************************************************
function m_cpy_img($file_img_in,$file_img_out){
if ( file_exists( $file_img_in )) {
copy( $file_img_in, $file_img_out );
}
}
?>
<script type="text/javascript">
<!--
var flag = '<?php echo $msg; ?>';
if(flag == "OK"){
var container = parent.document.getElementById('container1');
image = parent.document.createElement('img');
image.src = '<?php if($upname){ print($upname); }else{ print("../file/photo/no-img.jpg"); }?>';
parent.document.planform.pl_img1.value='<?php if($upname){ print($upname); }?>';
container.innerHTML = '';
container.appendChild(image);
}else{
alert(flag);
}
//-->
</script>
</body>
</html>
<!-----------------HTML----------------------->
<tr>
<th>
プラン 料理画像※
</th>
<td>
<iframe name="uploader1" src="" style="width:0px;height:0px;border:0px;"></iframe>
<form action="uploader1.php" method="post" enctype="multipart/form-data" target="uploader1">
<input type="hidden" name="max_file_size" value="1000000" />
<input type="file" name="upload_image" />
<input type="submit" value="料理画像アップロード"/>
</form>
<!--------画像表示先---------->
<div id="container1">
{if $dataList.pl_img1}<img src="{$dataList.pl_img1}" id="pl_img1">{/if}
</div>
</td>
</tr>
2012年7月11日水曜日
MYSQLで配列を格納検索を行うテスト
/*MYSQLで配列*/
/*********テーブルの作成************/
create table tbl1
(id int AUTO_INCREMENT ,
name varchar(10),
lng SET('1','2','3','4','5','6'),
PRIMARY KEY ( `id` )
)
/****************データの挿入******************/
INSERT INTO tbl1 (name,lng) values ('a','1,2');
INSERT INTO tbl1 (name,lng) values ('b','2');
INSERT INTO tbl1 (name,lng) values ('c','2,3');
INSERT INTO tbl1 (name,lng) values ('d','2,4');
/*************************検索**************************/
/*検索 完全一致*/
SELECT * FROM tbl1 WHERE lng LIKE '%1,4%';
/*検索 配列の値複数詮索*/
SELECT * FROM tbl1 WHERE lng = '1,2';
/*配列に値があれば*/
SELECT * FROM tbl1 WHERE lng & '1';
/*********テーブルの作成************/
create table tbl1
(id int AUTO_INCREMENT ,
name varchar(10),
lng SET('1','2','3','4','5','6'),
PRIMARY KEY ( `id` )
)
/****************データの挿入******************/
INSERT INTO tbl1 (name,lng) values ('a','1,2');
INSERT INTO tbl1 (name,lng) values ('b','2');
INSERT INTO tbl1 (name,lng) values ('c','2,3');
INSERT INTO tbl1 (name,lng) values ('d','2,4');
/*************************検索**************************/
/*検索 完全一致*/
SELECT * FROM tbl1 WHERE lng LIKE '%1,4%';
/*検索 配列の値複数詮索*/
SELECT * FROM tbl1 WHERE lng = '1,2';
/*配列に値があれば*/
SELECT * FROM tbl1 WHERE lng & '1';
2012年6月9日土曜日
2012年6月5日火曜日
PHP ファイルダウンロード
<?php // ダウンロードさせるファイル名 $tmp_file = "test.txt"; $j_file = "テスト.txt"; $j_file = mb_convert_encoding($j_file, "SJIS", "EUC"); // ヘッダ header("Content-Type: application/octet-stream"); // ダイアログボックスに表示するファイル名 header("Content-Disposition: attachment; filename=$j_file"); // 対象ファイルを出力する。 readfile($tmp_file); exit; ?>
2012年4月15日日曜日
ヒントsql
//a1_idが2でa1_formの配列1次元配列の最大要素数の値とa2_idが同じものを抽出
SELECT * FROM a1,a2
where a1.a1_id = 2
AND cast(array_upper(a1.a1_form,1)as integer) = a2.a2_id
SELECT * FROM a1,a2
where a1.a1_id = 2
AND cast(array_upper(a1.a1_form,1)as integer) = a2.a2_id
2012年4月2日月曜日
2012年3月16日金曜日
2012年3月15日木曜日
.htaccessでPCと携帯とスマフォ判定
<IfModule mod_rewrite.c>
RewriteEngine On
#######スマフォサイト########
# iPod
RewriteCond %{HTTP_USER_AGENT} (iPod|iPhone|DDIPOKET|iPad|J-Android|Vodafone|SoftBank)
RewriteRule ^$ /morimoto/test/sp/ [R]
#######携帯サイト########
RewriteCond %{HTTP_USER_AGENT} (DoCoMo|KDDI|DDIPOKET|UP\.Browser|J-PHONE|Vodafone|SoftBank)
RewriteRule ^$ /morimoto/test/mb/ [R]
#######PCサイト########
RewriteRule ^$ /morimoto/test/pc/ [R]
</IfModule>
RewriteEngine On
#######スマフォサイト########
# iPod
RewriteCond %{HTTP_USER_AGENT} (iPod|iPhone|DDIPOKET|iPad|J-Android|Vodafone|SoftBank)
RewriteRule ^$ /morimoto/test/sp/ [R]
#######携帯サイト########
RewriteCond %{HTTP_USER_AGENT} (DoCoMo|KDDI|DDIPOKET|UP\.Browser|J-PHONE|Vodafone|SoftBank)
RewriteRule ^$ /morimoto/test/mb/ [R]
#######PCサイト########
RewriteRule ^$ /morimoto/test/pc/ [R]
</IfModule>
2012年2月20日月曜日
php ture false nullて
$hantei_txt = array(
array("","ダブルクオート空文字")
,array('',"シングルオート空文字")
,array(0,"数値ゼロ")
,array(1,"数値1")
,array(01,"数値01")
,array("0","文字列ゼロ")
,array("1","文字列イチ")
,array(TURE,"ture")
,array(FALSE,"false")
,array("TURE","文字列 ture")
,array("FALSE","文字列 false")
,array(null," NULL")
,array("null","文字列NULL")
);
$count_num = count($hantei_txt);
for($i = 0; $i < $count_num; $i++){ if($hantei_txt[$i][0]){ echo $hantei_txt[$i][1]."="; echo "hit"; }else{ echo $hantei_txt[$i][1]."="; echo "none"; } echo "
";
}
ダブルクオート空文字=none
シングルオート空文字=none
数値ゼロ=none
数値1=hit
数値01=hit
文字列ゼロ=none
文字列イチ=hit
ture=hit
false=none
文字列 ture=hit
文字列 false=hit
NULL=none
文字列NULL=hit
array("","ダブルクオート空文字")
,array('',"シングルオート空文字")
,array(0,"数値ゼロ")
,array(1,"数値1")
,array(01,"数値01")
,array("0","文字列ゼロ")
,array("1","文字列イチ")
,array(TURE,"ture")
,array(FALSE,"false")
,array("TURE","文字列 ture")
,array("FALSE","文字列 false")
,array(null," NULL")
,array("null","文字列NULL")
);
$count_num = count($hantei_txt);
for($i = 0; $i < $count_num; $i++){ if($hantei_txt[$i][0]){ echo $hantei_txt[$i][1]."="; echo "hit"; }else{ echo $hantei_txt[$i][1]."="; echo "none"; } echo "
";
}
ダブルクオート空文字=none
シングルオート空文字=none
数値ゼロ=none
数値1=hit
数値01=hit
文字列ゼロ=none
文字列イチ=hit
ture=hit
false=none
文字列 ture=hit
文字列 false=hit
NULL=none
文字列NULL=hit
2012年2月19日日曜日
sql nullでないものを返す
使い方は引数の中でNULLでないもの返すので、たとえばこんな感じ
//セレクトとき
select COALESCE(カラム1, カラム2,カラム3) from user_master
//インサートとき
INSERT INTO
`tr_a`
(`a_id`,
`a_num`)
VALUES
(null,
COALESCE(null,0)
)
//セレクトとき
select COALESCE(カラム1, カラム2,カラム3) from user_master
//インサートとき
INSERT INTO
`tr_a`
(`a_id`,
`a_num`)
VALUES
(null,
COALESCE(null,0)
)
2012年2月12日日曜日
2012年1月16日月曜日
smartyで文字列加工
修飾子
変数が空の場合のデフォルト値{"test1"|default:"????"}
{""|default:"????"}
エスケープ
{"
{"'
htmlタグの削除
{"test"|strip_tags}
時間
{$smarty.now}{$smarty.now+60*60*24|date_format:"%w"}
{$smarty.now|date_format:"%Y年%m月%d日%h時%M分%S秒"}
改行文字を変換
{$data|nl2br}
スペース
を変換
{$data|strip:""}
テンプレート内で変数を生成
{assign var="num" value="99999"}
テンプレート内の値は{$num}
smarty オブジェクトで操作
require_once('MySmarty.php');
require_once('user_bean.php');
$uses=array();
$uses[] = new user_bean(1,"test_1","2011/01/01");
$uses[] = new user_bean(2,"test_2","2010/01/01");
$uses[] = new user_bean(3,"test_3","2009/01/01");
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('uses',$uses);
$mysmarty->display('index.tpl');
?>
//beanクラス
<?php
class user_bean{
//プロパティ
public $name;
public $id;
public $birth;
//メソッド
function __construct($id,$name,$birth){
$this->id = $id;
$this->name = $name;
$this->birth = $birth;
}
public function getage(){
return floor((time()-strtotime($this->birth))/(60*60*24*365));
}
}
?>
//テンプレート
{foreach from=$uses item=use}
<p>id {$use->id}</p>
<p>名前 {$use->name}</p>
<p>歳 {$use->getage()}</p>
{/foreach}
require_once('user_bean.php');
$uses=array();
$uses[] = new user_bean(1,"test_1","2011/01/01");
$uses[] = new user_bean(2,"test_2","2010/01/01");
$uses[] = new user_bean(3,"test_3","2009/01/01");
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('uses',$uses);
$mysmarty->display('index.tpl');
?>
//beanクラス
<?php
class user_bean{
//プロパティ
public $name;
public $id;
public $birth;
//メソッド
function __construct($id,$name,$birth){
$this->id = $id;
$this->name = $name;
$this->birth = $birth;
}
public function getage(){
return floor((time()-strtotime($this->birth))/(60*60*24*365));
}
}
?>
//テンプレート
{foreach from=$uses item=use}
<p>id {$use->id}</p>
<p>名前 {$use->name}</p>
<p>歳 {$use->getage()}</p>
{/foreach}
Smarty 連想配列
//プログラム
<?php
require_once('MySmarty.php');
require_once('DB.php');
$dbh = DB::connect('mysql://root:mysql@127.0.0.1/test');
$data = array( "id" => 1,
"shimei"=>array("ue"=>"yamada",
"shita"=>"taro")
);
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('data',$data);
$mysmarty->display('index.tpl');
?>
//テンプレート
<h1>{$msg}</h1>
{$data.id}<br />
{$data.shimei.ue}<br />
{$data.shimei.shita}<br />
//表示
yamada
taro
<?php
require_once('MySmarty.php');
require_once('DB.php');
$dbh = DB::connect('mysql://root:mysql@127.0.0.1/test');
$data = array( "id" => 1,
"shimei"=>array("ue"=>"yamada",
"shita"=>"taro")
);
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('data',$data);
$mysmarty->display('index.tpl');
?>
//テンプレート
<h1>{$msg}</h1>
{$data.id}<br />
{$data.shimei.ue}<br />
{$data.shimei.shita}<br />
//表示
hello MySmarty
1yamada
taro
Smarty DBからレコードを取り出し、テンプレート表示 テスト
mysql> insert into test_1(id,name) values(1,'test'),(2,'test2');
Query OK, 1 row affected (0.05 sec)
//レコードの確認
mysql> select * from test_1;
+------+-------+
| id | name |
+------+-------+
| 1 | test |
| 2 | test2 |
//プログラム
<?php
$dbh = DB::connect('mysql://root:mysql@127.0.0.1/test');
//sql文の作成
$sql = $dbh->prepare("SELECT * FROM test_1");
$data = array();
$rs = $dbh->execute($sql);
//配列へ挿入
while($row = $rs->fetchRow(DB_FETCHMODE_ASSOC)){
$data[]=array("id" => $row['id'],"name" => $row['name']);
}
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('data',$data);
$mysmarty->display('index.tpl');
?>
//テンプレート部分
<h1>{$msg}</h1>
{foreach from=$data item=datum}
<p>{$datum.id}</p>
<p>{$datum.name}</p>
{/foreach}
Query OK, 1 row affected (0.05 sec)
//レコードの確認
mysql> select * from test_1;
+------+-------+
| id | name |
+------+-------+
| 1 | test |
| 2 | test2 |
//プログラム
<?php
$dbh = DB::connect('mysql://root:mysql@127.0.0.1/test');
//sql文の作成
$sql = $dbh->prepare("SELECT * FROM test_1");
$data = array();
$rs = $dbh->execute($sql);
//配列へ挿入
while($row = $rs->fetchRow(DB_FETCHMODE_ASSOC)){
$data[]=array("id" => $row['id'],"name" => $row['name']);
}
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('data',$data);
$mysmarty->display('index.tpl');
?>
//テンプレート部分
<h1>{$msg}</h1>
{foreach from=$data item=datum}
<p>{$datum.id}</p>
<p>{$datum.name}</p>
{/foreach}
smarty 入門
/*************************************************/
/* 仕事でSmartyを使うんでおさらい */
/************************************************/
/*-----------ダウンロード---------*/
「http://www.smarty.net/download」より「Smarty 2.6.26」をダウンロード
/*-----------インストール----------*/
「Smarty 2.6.26」を展開し「Smarty」とリネイムし、
今回は「Windows」+「Ampps」環境なので「C:\Ampps\Smarty」に配置する。
/*------------php.iniの設定----------*/
//下記を編集
include_path = ".;C:\ampps\php\includes;C:\Ampps\Smarty\lib"
/*------------ディレクトリの配置----------------------*/
/*下記のように配置します。*/
smarty_sample-
|-test_app_1
|-templates //テンプレートファイル設置
|-templates_c //コンパイルしたテンプレート設置
|-configs //コンフィグファイル設置
|-cache //キャッシュ
/*--------------テンプレートの作成---------------------------------*/
下記のhtmlソースを「templates_c\index.tpl」に配置、
{*---- テストテンプレート ----*}
<html>
<head>
<title>Test_1</title>
</head>
<body>
<h1>{$msg}</h1>
</body>
</html>
/*--------------プログラムの作成---------------------------------*/
/*下記を「C:\Ampps\www\smarty_sample\test_app_1」に配置*/
<?php
{*require_once('C:/Ampps/Smarty/libs/Smarty.class.php');*}
require_once('marty.class.php');
$smarty = new Smarty();
$smarty->template_dir = 'C:\Ampps\smarty_sample\test_app_1/templates/';
$smarty->compile_dir = 'C:\Ampps\smarty_sample\test_app_1/templates_c/';
$smarty->config_dir = 'C:\Ampps\smarty_sample\test_app_1/configs/';
$smarty->cache_dir = 'C:\Ampps\smarty_sample\test_app_1/cache/';
$smarty->assign('msg','Hello World!');
$smarty->display('index.tpl');
?>
/* 仕事でSmartyを使うんでおさらい */
/************************************************/
/*-----------ダウンロード---------*/
「http://www.smarty.net/download」より「Smarty 2.6.26」をダウンロード
/*-----------インストール----------*/
「Smarty 2.6.26」を展開し「Smarty」とリネイムし、
今回は「Windows」+「Ampps」環境なので「C:\Ampps\Smarty」に配置する。
/*------------php.iniの設定----------*/
//下記を編集
include_path = ".;C:\ampps\php\includes;C:\Ampps\Smarty\lib"
/*------------ディレクトリの配置----------------------*/
/*下記のように配置します。*/
smarty_sample-
|-test_app_1
|-templates //テンプレートファイル設置
|-templates_c //コンパイルしたテンプレート設置
|-configs //コンフィグファイル設置
|-cache //キャッシュ
/*--------------テンプレートの作成---------------------------------*/
下記のhtmlソースを「templates_c\index.tpl」に配置、
{*---- テストテンプレート ----*}
<html>
<head>
<title>Test_1</title>
</head>
<body>
<h1>{$msg}</h1>
</body>
</html>
/*--------------プログラムの作成---------------------------------*/
/*下記を「C:\Ampps\www\smarty_sample\test_app_1」に配置*/
<?php
{*require_once('C:/Ampps/Smarty/libs/Smarty.class.php');*}
require_once('marty.class.php');
$smarty = new Smarty();
$smarty->template_dir = 'C:\Ampps\smarty_sample\test_app_1/templates/';
$smarty->compile_dir = 'C:\Ampps\smarty_sample\test_app_1/templates_c/';
$smarty->config_dir = 'C:\Ampps\smarty_sample\test_app_1/configs/';
$smarty->cache_dir = 'C:\Ampps\smarty_sample\test_app_1/cache/';
$smarty->assign('msg','Hello World!');
$smarty->display('index.tpl');
?>
2012年1月11日水曜日
phpとmysqlでテーブルの存在確認
<?php
$host = "localhost";
$user = "root";
$pass = "mysql";
$db = "test";
$con = mysql_connect($host,$user,$pass) or die("db接続NG");
print "db接続OK";
//テーブルチェック関数の呼出
if(table_exists($db,'item',$con)){
print "item table hit";
}else{
print "item table none";
}
mysql_close();
// テーブルの存在チェック関数の定義 start
function table_exists($db_name,$tbl_name,$db)
{
// テーブルリストの取得
$rs = mysql_query("SHOW TABLES FROM $db_name");
while($arr_row = mysql_fetch_row($rs)){
if(in_array($tbl_name,$arr_row)){
return true;
}
}
return false;
}
// テーブルの存在チェック関数の定義 end
?>
$host = "localhost";
$user = "root";
$pass = "mysql";
$db = "test";
$con = mysql_connect($host,$user,$pass) or die("db接続NG");
print "db接続OK";
//テーブルチェック関数の呼出
if(table_exists($db,'item',$con)){
print "item table hit";
}else{
print "item table none";
}
mysql_close();
// テーブルの存在チェック関数の定義 start
function table_exists($db_name,$tbl_name,$db)
{
// テーブルリストの取得
$rs = mysql_query("SHOW TABLES FROM $db_name");
while($arr_row = mysql_fetch_row($rs)){
if(in_array($tbl_name,$arr_row)){
return true;
}
}
return false;
}
// テーブルの存在チェック関数の定義 end
?>
MYSQL 入門4
-------------------ストアード-----------------
DELIMITER //
CREATE PROCEDURE TEST_1()
BEGIN
SELECT * FROM C1;
END
//
DELIMITER ;
CALL TEST_1;--ストアドの呼出
+----+---------------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID |
+----+---------------------+------------------+
| 1 | BOOK | NULL |
| 2 | Fashion | NULL |
| 3 | electronics | NULL |
| 4 | Picture BOOK | 1 |
| 5 | IT BOOK | 1 |
| 6 | COOKING BOOK | 1 |
| 7 | TV electronics | 3 |
| 8 | Music electronics | 3 |
| 9 | Cooking electronics | 3 |
| 10 | Man Fashion | 2 |
| 11 | WOMAN Fashion | 2 |
| 12 | TEST | 100 |
+----+---------------------+------------------+
------------引数ありのストアド----------------
DELIMITER //
CREATE PROCEDURE TEST_2(OUT o_1 INT)
BEGIN
SELECT COUNT(*) INTO o_1 FROM C1;
END
//
mysql> CALL TEST_2(@i)--ストアドの呼出
-> //
Query OK, 1 row affected (0.00 sec)
mysql> SELECT @i--戻りのOUTを表示
-> //
+------+
| @i |
+------+
| 12 |
+------+
---------ストアードファンクション------------
DELIMITER //
CREATE FUNCTION tEST9()
RETURNS DOUBLE
BEGIN
DECLARE r DOUBLE;
SELECT AVG(ID) INTO r FROM C1;
RETURN r;
END
//
------------ストレージエンジン---------------
/*MyISAM(マイアイサム) もっともよく利用される高速エンジン トランザクションは利用できない*/
/*InnoDB(イノディービー) トランザクションに対応*/
/*ストレージエンジンの確認*/
| tb1 | CREATE TABLE `tb1` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
/*レコードの確認*/
mysql> select * from tb1;
+------+-------+
| id | name |
+------+-------+
| 1 | test1 |
+------+-------+
1 row in set (0.00 sec)
/*トランザクションのスタート*/
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tb1;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb1;
Empty set (0.00 sec)
/*ロールバック*/
mysql> rollback;
Query OK, 0 rows affected (0.05 sec)
/*レコード確認*/
mysql> select * from tb1;
+------+-------+
| id | name |
+------+-------+
| 1 | test1 |
+------+-------+
/*--------------ファイルの取り扱い--------------------*/
CREATE TABLE address_zip (
jis varchar(10) NULL,
zip_old varchar(5) NULL,
zip varchar(7) NULL,
addr1_kana varchar(100) NULL,
addr2_kana varchar(100) NULL,
addr3_kana varchar(100) NULL,
addr1 varchar(100) NULL,
addr2 varchar(100) NULL,
addr3 varchar(100) NULL,
c1 int NULL,
c2 int NULL,
c3 int NULL,
c4 int NULL,
c5 int NULL,
c6 int NULL
);
LOAD DATA LOCAL INFILE 'C:/ken_all/KEN_ALL.CSV'
INTO TABLE `address_zip`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"';
SELECT * INTO OUTFILE 'C:/DATA.CSV'
FIELDS TERMINATED BY ','
FROM ADDRESS_ZIP;
/*--------------バッチファイル--------------------*/
/*下記test.batで保存し実行するとdataファイルが作成されます*/
mysql test -uroot -pmysql -e "SELECT * INTO OUTFILE 'C:/DATA2.CSV' FIELDS TERMINATED BY ',' FROM ADDRESS_ZIP;"
/*--------------リダイレクト処理--------------------*/
/*下記はc直下にlog.txtファイルに実行結果を出力します*/
tee C:/log.txt /*出力開始*/
SELECT * FROM ADDRESS_ZIP LIMIT 5;
notee /*出力終了*/
/*-----------------------------------------------------------*/
/* バックアップ */
/*-----------------------------------------------------------*/
mysqldump -uroot -pmysql test>C:/back_up.txt;
DELIMITER //
CREATE PROCEDURE TEST_1()
BEGIN
SELECT * FROM C1;
END
//
DELIMITER ;
CALL TEST_1;--ストアドの呼出
+----+---------------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID |
+----+---------------------+------------------+
| 1 | BOOK | NULL |
| 2 | Fashion | NULL |
| 3 | electronics | NULL |
| 4 | Picture BOOK | 1 |
| 5 | IT BOOK | 1 |
| 6 | COOKING BOOK | 1 |
| 7 | TV electronics | 3 |
| 8 | Music electronics | 3 |
| 9 | Cooking electronics | 3 |
| 10 | Man Fashion | 2 |
| 11 | WOMAN Fashion | 2 |
| 12 | TEST | 100 |
+----+---------------------+------------------+
------------引数ありのストアド----------------
DELIMITER //
CREATE PROCEDURE TEST_2(OUT o_1 INT)
BEGIN
SELECT COUNT(*) INTO o_1 FROM C1;
END
//
mysql> CALL TEST_2(@i)--ストアドの呼出
-> //
Query OK, 1 row affected (0.00 sec)
mysql> SELECT @i--戻りのOUTを表示
-> //
+------+
| @i |
+------+
| 12 |
+------+
---------ストアードファンクション------------
DELIMITER //
CREATE FUNCTION tEST9()
RETURNS DOUBLE
BEGIN
DECLARE r DOUBLE;
SELECT AVG(ID) INTO r FROM C1;
RETURN r;
END
//
------------ストレージエンジン---------------
/*MyISAM(マイアイサム) もっともよく利用される高速エンジン トランザクションは利用できない*/
/*InnoDB(イノディービー) トランザクションに対応*/
/*ストレージエンジンの確認*/
| tb1 | CREATE TABLE `tb1` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
/*レコードの確認*/
mysql> select * from tb1;
+------+-------+
| id | name |
+------+-------+
| 1 | test1 |
+------+-------+
1 row in set (0.00 sec)
/*トランザクションのスタート*/
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from tb1;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb1;
Empty set (0.00 sec)
/*ロールバック*/
mysql> rollback;
Query OK, 0 rows affected (0.05 sec)
/*レコード確認*/
mysql> select * from tb1;
+------+-------+
| id | name |
+------+-------+
| 1 | test1 |
+------+-------+
/*--------------ファイルの取り扱い--------------------*/
CREATE TABLE address_zip (
jis varchar(10) NULL,
zip_old varchar(5) NULL,
zip varchar(7) NULL,
addr1_kana varchar(100) NULL,
addr2_kana varchar(100) NULL,
addr3_kana varchar(100) NULL,
addr1 varchar(100) NULL,
addr2 varchar(100) NULL,
addr3 varchar(100) NULL,
c1 int NULL,
c2 int NULL,
c3 int NULL,
c4 int NULL,
c5 int NULL,
c6 int NULL
);
LOAD DATA LOCAL INFILE 'C:/ken_all/KEN_ALL.CSV'
INTO TABLE `address_zip`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"';
SELECT * INTO OUTFILE 'C:/DATA.CSV'
FIELDS TERMINATED BY ','
FROM ADDRESS_ZIP;
/*--------------バッチファイル--------------------*/
/*下記test.batで保存し実行するとdataファイルが作成されます*/
mysql test -uroot -pmysql -e "SELECT * INTO OUTFILE 'C:/DATA2.CSV' FIELDS TERMINATED BY ',' FROM ADDRESS_ZIP;"
/*--------------リダイレクト処理--------------------*/
/*下記はc直下にlog.txtファイルに実行結果を出力します*/
tee C:/log.txt /*出力開始*/
SELECT * FROM ADDRESS_ZIP LIMIT 5;
notee /*出力終了*/
/*-----------------------------------------------------------*/
/* バックアップ */
/*-----------------------------------------------------------*/
mysqldump -uroot -pmysql test>C:/back_up.txt;
2012年1月6日金曜日
MYSQL 入門3
-------------LEFT JOIN--------------
mysql> SELECT * FROM USER;
+----+----------+------+---------+---------+
| ID | NAME | Unit | KOZUKAI | BIKOU |
+----+----------+------+---------+---------+
| 1 | ASO | 1 | 10000 | SUKUNAI |
| 2 | KATO | 4 | 90000 | OUI |
| 3 | YAMADA | 2 | 10000 | SUKUNAI |
| 4 | SUZUKI | 1 | 10000 | SUKUNAI |
| 5 | MIYAMOTO | 3 | 50000 | FUTUU |
| 6 | TANAKA | 4 | 90000 | OUI |
+----+----------+------+---------+---------+
mysql> SELECT * FROM UNIT;
+----+----------+
| ID | NAME |
+----+----------+
| 1 | SOUMU |
| 2 | EIGYO |
| 3 | JIMU |
| 4 | GIJYUTSU |
+----+----------+
---UNITテーブルのIDにないレコードを追加----
UPDATE USER SET UNIT = '7' WHERE ID = 6;
/*----LEFT JOINでは左のテーブルのレコードは全て表示されます。----*/
mysql> SELECT *
-> FROM USER
-> LEFT JOIN UNIT
-> ON USER.UNIT = UNIT.ID ;
+----+----------+------+---------+---------+------+----------+
| ID | NAME | Unit | KOZUKAI | BIKOU | ID | NAME |
+----+----------+------+---------+---------+------+----------+
| 1 | ASO | 1 | 10000 | SUKUNAI | 1 | SOUMU |
| 2 | KATO | 4 | 90000 | OUI | 4 | GIJYUTSU |
| 3 | YAMADA | 2 | 10000 | SUKUNAI | 2 | EIGYO |
| 4 | SUZUKI | 1 | 10000 | SUKUNAI | 1 | SOUMU |
| 5 | MIYAMOTO | 3 | 50000 | FUTUU | 3 | JIMU |
| 6 | TANAKA | 7 | 90000 | OUI | NULL | NULL |
+----+----------+------+---------+---------+------+----------+
/*----LEFT RIGHTでは右のテーブルのレコードは全て表示されます。USER.UNIT = UNIT.IDが一致しない
レコードは表示されません----*/
mysql> SELECT *
-> FROM USER
-> RIGHT JOIN UNIT
-> ON USER.UNIT = UNIT.ID ;
+------+----------+------+---------+---------+----+----------+
| ID | NAME | Unit | KOZUKAI | BIKOU | ID | NAME |
+------+----------+------+---------+---------+----+----------+
| 1 | ASO | 1 | 10000 | SUKUNAI | 1 | SOUMU |
| 4 | SUZUKI | 1 | 10000 | SUKUNAI | 1 | SOUMU |
| 3 | YAMADA | 2 | 10000 | SUKUNAI | 2 | EIGYO |
| 5 | MIYAMOTO | 3 | 50000 | FUTUU | 3 | JIMU |
| 2 | KATO | 4 | 90000 | OUI | 4 | GIJYUTSU |
+------+----------+------+---------+---------+----+----------+
-------------------------自己結合----------------------------
/*---カテゴリテーブルの作成---*/
CREATE TABLE Category (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100),HOYA_CATEGORY_ID INT);
INSERT INTO CATEGORY(NAME,HOYA_CATEGORY_ID) VALUES('BOOK',NULL),('Fashion',NULL),('electronics',NULL);--親カテゴリ
INSERT INTO CATEGORY(NAME,HOYA_CATEGORY_ID) VALUES('Picture BOOK',1),('IT BOOK',1),('COOKING BOOK',1);--子カテゴリ
INSERT INTO CATEGORY(NAME,HOYA_CATEGORY_ID) VALUES('Man Fashion',2),('WOMAN Fashion',2);--子カテゴリ
INSERT INTO CATEGORY(NAME,HOYA_CATEGORY_ID) VALUES('TV electronics',3),('Music electronics',3),('Cooking electronics',3);--子カテゴリ
mysql> SELECT * FROM CATEGORY;
+----+---------------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID |
+----+---------------------+------------------+
| 1 | BOOK | NULL |
| 2 | Fashion | NULL |
| 3 | electronics | NULL |
| 4 | Picture BOOK | 1 |
| 5 | IT BOOK | 1 |
| 6 | COOKING BOOK | 1 |
| 7 | TV electronics | 3 |
| 8 | Music electronics | 3 |
| 9 | Cooking electronics | 3 |
| 10 | Man Fashion | 2 |
| 11 | WOMAN Fashion | 2 |
+----+---------------------+------------------+
--------自己結合によるカテゴリーの表示----------
mysql> SELECT *
-> FROM CATEGORY
-> AS C_1
-> JOIN CATEGORY
-> AS C_2
-> WHERE C_1.ID = C_2.HOYA_CATEGORY_ID;
+----+-------------+------------------+----+---------------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID | ID | NAME | HOYA_CATEGORY_ID |
+----+-------------+------------------+----+---------------------+------------------+
| 1 | BOOK | NULL | 4 | Picture BOOK | 1 |
| 1 | BOOK | NULL | 5 | IT BOOK | 1 |
| 1 | BOOK | NULL | 6 | COOKING BOOK | 1 |
| 3 | electronics | NULL | 7 | TV electronics | 3 |
| 3 | electronics | NULL | 8 | Music electronics | 3 |
| 3 | electronics | NULL | 9 | Cooking electronics | 3 |
| 2 | Fashion | NULL | 10 | Man Fashion | 2 |
| 2 | Fashion | NULL | 11 | WOMAN Fashion | 2 |
+----+-------------+------------------+----+---------------------+------------------+
---------------------------------サブクエリー------------------------------
/*サブクエリで最大IDを取得*/
mysql> SELECT * FROM CATEGORY WHERE ID IN (SELECT MAX(ID) FROM CATEGORY);
+----+---------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID |
+----+---------------+------------------+
| 11 | WOMAN Fashion | 2 |
+----+---------------+------------------+
mysql> SELECT * FROM CATEGORY WHERE ID IN (SELECT MAX(HOYA_CATEGORY_ID) FROM CATEGORY);
+----+-------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID |
+----+-------------+------------------+
| 3 | electronics | NULL |
+----+-------------+------------------+
------------商品のカテゴリを表示-------------
CREATE TABLE PRODUCT(ID INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(100),CATEGORY_ID INT);
INSERT INTO PRODUCT(NAME,CATEGORY_ID) VALUES('PC_WORK',5),('IT_WORK',5),('PAN_COOK',6),('CAMERA',4);
mysql> SELECT CAT.NAME AS 'カテゴリ',PRO.NAME AS '商品名'FROM CATEGORY AS CAT ,PRODUCT AS PRO WHERE CAT.ID = PRO.CATEGORY_ID;
+--------------+----------+
| カテゴリ | 商品名 |
+--------------+----------+
| IT BOOK | PC_WORK |
| IT BOOK | IT_WORK |
| COOKING BOOK | PAN_COOK |
| Picture BOOK | CAMERA |
+--------------+----------+
----------商品が無いカテゴリを表示-----------
SELECT CATEGORY.NAME
FROM CATEGORY
WHERE NOT EXISTS (SELECT * FROM PRODUCT WHERE PRODUCT.CATEGORY_ID = CATEGORY.ID)
AND CATEGORY.HOYA_CATEGORY_ID IS NOT NULL
;
+---------------------+
| NAME |
+---------------------+
| TV electronics |
| Music electronics |
| Cooking electronics |
| Man Fashion |
| WOMAN Fashion |
| TEST |
+---------------------+
----------------カテゴリの商品件数-----------------
SELECT CAT.NAME,COUNT(*)
FROM CATEGORY AS CAT,
PRODUCT AS PRO
WHERE CAT.ID = PRO.CATEGORY_ID
GROUP BY CAT.ID;
+--------------+----------+
| NAME | COUNT(*) |
+--------------+----------+
| Picture BOOK | 1 |
| IT BOOK | 2 |
| COOKING BOOK | 1 |
+--------------+----------+
----------------ビュー-----------------
CREATE VIEW C1
AS
SELECT ID,NAME
FROM CATEGORY;
SELECT * FROM C1;
+----+---------------------+
| ID | NAME |
+----+---------------------+
| 1 | BOOK |
| 2 | Fashion |
| 3 | electronics |
| 4 | Picture BOOK |
| 5 | IT BOOK |
| 6 | COOKING BOOK |
| 7 | TV electronics |
| 8 | Music electronics |
| 9 | Cooking electronics |
| 10 | Man Fashion |
| 11 | WOMAN Fashion |
| 12 | TEST |
+----+---------------------+
--------自己結合したテーブルのVIEW---------
/*ポイントは同じなのカラム名にならない事*/
mysql> CREATE VIEW C2 AS
-> SELECT
-> CAT.ID,
-> CAT.NAME,
-> C1.ID AS KO_ID,
-> C1.NAME AS KO_NAME
-> FROM CATEGORY AS CAT
-> JOIN C1
-> WHERE CAT.ID = C1.HOYA_CATEGORY_ID;
Query OK, 0 rows affected (0.08 sec)
mysql> SELECT * FROM C2;
+----+-------------+-------+---------------------+
| ID | NAME | KO_ID | KO_NAME |
+----+-------------+-------+---------------------+
| 1 | BOOK | 4 | Picture BOOK |
| 1 | BOOK | 5 | IT BOOK |
| 1 | BOOK | 6 | COOKING BOOK |
| 3 | electronics | 7 | TV electronics |
| 3 | electronics | 8 | Music electronics |
| 3 | electronics | 9 | Cooking electronics |
| 2 | Fashion | 10 | Man Fashion |
| 2 | Fashion | 11 | WOMAN Fashion |
+----+-------------+-------+---------------------+
-------------ビュー上書き-------------
CREATE OR REPLACE VIEW C2 AS SELECT NOW();
-------------ビュのカラム構造の変更-------------
ALTER VIEW C2 AS
SELECT
CAT.ID,
CAT.NAME,
C1.ID AS KO_ID,
C1.NAME AS KO_NAME
FROM CATEGORY AS CAT
JOIN C1
WHERE CAT.ID = C1.HOYA_CATEGORY_ID;
-------------ビュ-の削除-------------
/*VIEWがあれば削除します。*/
DROP VIEW IF EXISTS C2;
mysql> SELECT * FROM USER;
+----+----------+------+---------+---------+
| ID | NAME | Unit | KOZUKAI | BIKOU |
+----+----------+------+---------+---------+
| 1 | ASO | 1 | 10000 | SUKUNAI |
| 2 | KATO | 4 | 90000 | OUI |
| 3 | YAMADA | 2 | 10000 | SUKUNAI |
| 4 | SUZUKI | 1 | 10000 | SUKUNAI |
| 5 | MIYAMOTO | 3 | 50000 | FUTUU |
| 6 | TANAKA | 4 | 90000 | OUI |
+----+----------+------+---------+---------+
mysql> SELECT * FROM UNIT;
+----+----------+
| ID | NAME |
+----+----------+
| 1 | SOUMU |
| 2 | EIGYO |
| 3 | JIMU |
| 4 | GIJYUTSU |
+----+----------+
---UNITテーブルのIDにないレコードを追加----
UPDATE USER SET UNIT = '7' WHERE ID = 6;
/*----LEFT JOINでは左のテーブルのレコードは全て表示されます。----*/
mysql> SELECT *
-> FROM USER
-> LEFT JOIN UNIT
-> ON USER.UNIT = UNIT.ID ;
+----+----------+------+---------+---------+------+----------+
| ID | NAME | Unit | KOZUKAI | BIKOU | ID | NAME |
+----+----------+------+---------+---------+------+----------+
| 1 | ASO | 1 | 10000 | SUKUNAI | 1 | SOUMU |
| 2 | KATO | 4 | 90000 | OUI | 4 | GIJYUTSU |
| 3 | YAMADA | 2 | 10000 | SUKUNAI | 2 | EIGYO |
| 4 | SUZUKI | 1 | 10000 | SUKUNAI | 1 | SOUMU |
| 5 | MIYAMOTO | 3 | 50000 | FUTUU | 3 | JIMU |
| 6 | TANAKA | 7 | 90000 | OUI | NULL | NULL |
+----+----------+------+---------+---------+------+----------+
/*----LEFT RIGHTでは右のテーブルのレコードは全て表示されます。USER.UNIT = UNIT.IDが一致しない
レコードは表示されません----*/
mysql> SELECT *
-> FROM USER
-> RIGHT JOIN UNIT
-> ON USER.UNIT = UNIT.ID ;
+------+----------+------+---------+---------+----+----------+
| ID | NAME | Unit | KOZUKAI | BIKOU | ID | NAME |
+------+----------+------+---------+---------+----+----------+
| 1 | ASO | 1 | 10000 | SUKUNAI | 1 | SOUMU |
| 4 | SUZUKI | 1 | 10000 | SUKUNAI | 1 | SOUMU |
| 3 | YAMADA | 2 | 10000 | SUKUNAI | 2 | EIGYO |
| 5 | MIYAMOTO | 3 | 50000 | FUTUU | 3 | JIMU |
| 2 | KATO | 4 | 90000 | OUI | 4 | GIJYUTSU |
+------+----------+------+---------+---------+----+----------+
-------------------------自己結合----------------------------
/*---カテゴリテーブルの作成---*/
CREATE TABLE Category (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100),HOYA_CATEGORY_ID INT);
INSERT INTO CATEGORY(NAME,HOYA_CATEGORY_ID) VALUES('BOOK',NULL),('Fashion',NULL),('electronics',NULL);--親カテゴリ
INSERT INTO CATEGORY(NAME,HOYA_CATEGORY_ID) VALUES('Picture BOOK',1),('IT BOOK',1),('COOKING BOOK',1);--子カテゴリ
INSERT INTO CATEGORY(NAME,HOYA_CATEGORY_ID) VALUES('Man Fashion',2),('WOMAN Fashion',2);--子カテゴリ
INSERT INTO CATEGORY(NAME,HOYA_CATEGORY_ID) VALUES('TV electronics',3),('Music electronics',3),('Cooking electronics',3);--子カテゴリ
mysql> SELECT * FROM CATEGORY;
+----+---------------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID |
+----+---------------------+------------------+
| 1 | BOOK | NULL |
| 2 | Fashion | NULL |
| 3 | electronics | NULL |
| 4 | Picture BOOK | 1 |
| 5 | IT BOOK | 1 |
| 6 | COOKING BOOK | 1 |
| 7 | TV electronics | 3 |
| 8 | Music electronics | 3 |
| 9 | Cooking electronics | 3 |
| 10 | Man Fashion | 2 |
| 11 | WOMAN Fashion | 2 |
+----+---------------------+------------------+
--------自己結合によるカテゴリーの表示----------
mysql> SELECT *
-> FROM CATEGORY
-> AS C_1
-> JOIN CATEGORY
-> AS C_2
-> WHERE C_1.ID = C_2.HOYA_CATEGORY_ID;
+----+-------------+------------------+----+---------------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID | ID | NAME | HOYA_CATEGORY_ID |
+----+-------------+------------------+----+---------------------+------------------+
| 1 | BOOK | NULL | 4 | Picture BOOK | 1 |
| 1 | BOOK | NULL | 5 | IT BOOK | 1 |
| 1 | BOOK | NULL | 6 | COOKING BOOK | 1 |
| 3 | electronics | NULL | 7 | TV electronics | 3 |
| 3 | electronics | NULL | 8 | Music electronics | 3 |
| 3 | electronics | NULL | 9 | Cooking electronics | 3 |
| 2 | Fashion | NULL | 10 | Man Fashion | 2 |
| 2 | Fashion | NULL | 11 | WOMAN Fashion | 2 |
+----+-------------+------------------+----+---------------------+------------------+
---------------------------------サブクエリー------------------------------
/*サブクエリで最大IDを取得*/
mysql> SELECT * FROM CATEGORY WHERE ID IN (SELECT MAX(ID) FROM CATEGORY);
+----+---------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID |
+----+---------------+------------------+
| 11 | WOMAN Fashion | 2 |
+----+---------------+------------------+
mysql> SELECT * FROM CATEGORY WHERE ID IN (SELECT MAX(HOYA_CATEGORY_ID) FROM CATEGORY);
+----+-------------+------------------+
| ID | NAME | HOYA_CATEGORY_ID |
+----+-------------+------------------+
| 3 | electronics | NULL |
+----+-------------+------------------+
------------商品のカテゴリを表示-------------
CREATE TABLE PRODUCT(ID INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(100),CATEGORY_ID INT);
INSERT INTO PRODUCT(NAME,CATEGORY_ID) VALUES('PC_WORK',5),('IT_WORK',5),('PAN_COOK',6),('CAMERA',4);
mysql> SELECT CAT.NAME AS 'カテゴリ',PRO.NAME AS '商品名'FROM CATEGORY AS CAT ,PRODUCT AS PRO WHERE CAT.ID = PRO.CATEGORY_ID;
+--------------+----------+
| カテゴリ | 商品名 |
+--------------+----------+
| IT BOOK | PC_WORK |
| IT BOOK | IT_WORK |
| COOKING BOOK | PAN_COOK |
| Picture BOOK | CAMERA |
+--------------+----------+
----------商品が無いカテゴリを表示-----------
SELECT CATEGORY.NAME
FROM CATEGORY
WHERE NOT EXISTS (SELECT * FROM PRODUCT WHERE PRODUCT.CATEGORY_ID = CATEGORY.ID)
AND CATEGORY.HOYA_CATEGORY_ID IS NOT NULL
;
+---------------------+
| NAME |
+---------------------+
| TV electronics |
| Music electronics |
| Cooking electronics |
| Man Fashion |
| WOMAN Fashion |
| TEST |
+---------------------+
----------------カテゴリの商品件数-----------------
SELECT CAT.NAME,COUNT(*)
FROM CATEGORY AS CAT,
PRODUCT AS PRO
WHERE CAT.ID = PRO.CATEGORY_ID
GROUP BY CAT.ID;
+--------------+----------+
| NAME | COUNT(*) |
+--------------+----------+
| Picture BOOK | 1 |
| IT BOOK | 2 |
| COOKING BOOK | 1 |
+--------------+----------+
----------------ビュー-----------------
CREATE VIEW C1
AS
SELECT ID,NAME
FROM CATEGORY;
SELECT * FROM C1;
+----+---------------------+
| ID | NAME |
+----+---------------------+
| 1 | BOOK |
| 2 | Fashion |
| 3 | electronics |
| 4 | Picture BOOK |
| 5 | IT BOOK |
| 6 | COOKING BOOK |
| 7 | TV electronics |
| 8 | Music electronics |
| 9 | Cooking electronics |
| 10 | Man Fashion |
| 11 | WOMAN Fashion |
| 12 | TEST |
+----+---------------------+
--------自己結合したテーブルのVIEW---------
/*ポイントは同じなのカラム名にならない事*/
mysql> CREATE VIEW C2 AS
-> SELECT
-> CAT.ID,
-> CAT.NAME,
-> C1.ID AS KO_ID,
-> C1.NAME AS KO_NAME
-> FROM CATEGORY AS CAT
-> JOIN C1
-> WHERE CAT.ID = C1.HOYA_CATEGORY_ID;
Query OK, 0 rows affected (0.08 sec)
mysql> SELECT * FROM C2;
+----+-------------+-------+---------------------+
| ID | NAME | KO_ID | KO_NAME |
+----+-------------+-------+---------------------+
| 1 | BOOK | 4 | Picture BOOK |
| 1 | BOOK | 5 | IT BOOK |
| 1 | BOOK | 6 | COOKING BOOK |
| 3 | electronics | 7 | TV electronics |
| 3 | electronics | 8 | Music electronics |
| 3 | electronics | 9 | Cooking electronics |
| 2 | Fashion | 10 | Man Fashion |
| 2 | Fashion | 11 | WOMAN Fashion |
+----+-------------+-------+---------------------+
-------------ビュー上書き-------------
CREATE OR REPLACE VIEW C2 AS SELECT NOW();
-------------ビュのカラム構造の変更-------------
ALTER VIEW C2 AS
SELECT
CAT.ID,
CAT.NAME,
C1.ID AS KO_ID,
C1.NAME AS KO_NAME
FROM CATEGORY AS CAT
JOIN C1
WHERE CAT.ID = C1.HOYA_CATEGORY_ID;
-------------ビュ-の削除-------------
/*VIEWがあれば削除します。*/
DROP VIEW IF EXISTS C2;
2012年1月5日木曜日
MYSQL入門 2
-------------OFFSET--------------
mysql> SELECT * FROM TEST5;
+-----+---------------------+
| ID2 | TIMES2 |
+-----+---------------------+
| 1 | 2012-01-04 17:47:38 |
| 2 | 2012-01-04 17:47:38 |
| 3 | 2012-01-04 17:47:38 |
| 4 | 2012-01-04 17:47:38 |
| 5 | 2012-01-04 18:19:34 |
| 6 | 2012-01-04 18:19:34 |
| 7 | 2012-01-04 18:19:34 |
| 8 | 2012-01-04 18:19:34 |
+-----+---------------------+
mysql> SELECT * FROM TEST5 LIMIT 4 OFFSET 2;
+-----+---------------------+
| ID2 | TIMES2 |
+-----+---------------------+
| 3 | 2012-01-04 17:47:38 |
| 4 | 2012-01-04 17:47:38 |
| 5 | 2012-01-04 18:19:34 |
| 6 | 2012-01-04 18:19:34 |
+-----+---------------------+
---------------テストデータの作成------------------
mysql> CREATE TABLE USER(ID INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(20),Unit INT);
mysql> INSERT INTO USER(NAME,UNIT)VALUES('ASO',1),('KATO',4),('YAMADA',2),('SUZUKI',1),('MIYAMOTO',3),('TANAKA',4);
mysql> SELECT * FROM USER;
+----+----------+------+
| ID | NAME | Unit |
+----+----------+------+
| 1 | ASO | 1 |
| 2 | KATO | 4 |
| 3 | YAMADA | 2 |
| 4 | SUZUKI | 1 |
| 5 | MIYAMOTO | 3 |
| 6 | TANAKA | 4 |
+----+----------+------+
CREATE TABLE UNIT(ID INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(20));
INSERT INTO UNIT(NAME) VALUES('SOUMU'),('EIGYO'),('JIMU'),('GIJYUTSU');
mysql> SELECT * FROM UNIT;
+----+----------+
| ID | NAME |
+----+----------+
| 1 | SOUMU |
| 2 | EIGYO |
| 3 | JIMU |
| 4 | GIJYUTSU |
+----+----------+
-----グループ分けしカウント-----
mysql> SELECT UNIT.ID,UNIT.NAME ,COUNT(USER.UNIT) AS 'BUSYO_COUNT'
FROM USER
JOIN UNIT ON USER.UNIT = UNIT.ID
GROUP BY USER.UNIT;
+----+----------+-------------+
| ID | NAME | BUSYO_COUNT |
+----+----------+-------------+
| 1 | SOUMU | 2 |
| 2 | EIGYO | 1 |
| 3 | JIMU | 1 |
| 4 | GIJYUTSU | 2 |
+----+----------+-------------+
-----グループ分けした後の抽出-----
mysql> SELECT UNIT.ID,UNIT.NAME ,COUNT(USER.UNIT) AS 'BUSYO_COUNT'
-> FROM USER
-> JOIN UNIT ON USER.UNIT = UNIT.ID
-> GROUP BY USER.UNIT
-> HAVING BUSYO_COUNT >= 2;
+----+----------+-------------+
| ID | NAME | BUSYO_COUNT |
+----+----------+-------------+
| 1 | SOUMU | 2 |
| 4 | GIJYUTSU | 2 |
+----+----------+-------------+
-----------UPDATE-----------
ALTER TABLE USER ADD KOZUKAI INT;
mysql> SELECT * FROM USER;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | NULL |
| 2 | KATO | 4 | NULL |
| 3 | YAMADA | 2 | NULL |
| 4 | SUZUKI | 1 | NULL |
| 5 | MIYAMOTO | 3 | NULL |
| 6 | TANAKA | 4 | NULL |
+----+----------+------+---------+
UPDATE USER SET KOZUKAI = 10000;
mysql> SELECT * FROM USER;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | 10000 |
| 2 | KATO | 4 | 10000 |
| 3 | YAMADA | 2 | 10000 |
| 4 | SUZUKI | 1 | 10000 |
| 5 | MIYAMOTO | 3 | 10000 |
| 6 | TANAKA | 4 | 10000 |
+----+----------+------+---------+
--------条件が一致するレコードを修正---------
UPDATE USER SET KOZUKAI = 90000 WHERE UNIT = 4;
mysql> SELECT * FROM USER;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | 10000 |
| 2 | KATO | 4 | 90000 |
| 3 | YAMADA | 2 | 10000 |
| 4 | SUZUKI | 1 | 10000 |
| 5 | MIYAMOTO | 3 | 10000 |
| 6 | TANAKA | 4 | 90000 |
+----+----------+------+---------+
UPDATE USER SET KOZUKAI = 50000 WHERE UNIT = 3;
mysql> SELECT * FROM USER;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | 10000 |
| 2 | KATO | 4 | 90000 |
| 3 | YAMADA | 2 | 10000 |
| 4 | SUZUKI | 1 | 10000 |
| 5 | MIYAMOTO | 3 | 50000 |
| 6 | TANAKA | 4 | 90000 |
+----+----------+------+---------+
SELECT * FROM USER ORDER BY KOZUKAI;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | 10000 |
| 3 | YAMADA | 2 | 10000 |
| 4 | SUZUKI | 1 | 10000 |
| 5 | MIYAMOTO | 3 | 50000 |
| 2 | KATO | 4 | 90000 |
| 6 | TANAKA | 4 | 90000 |
+----+----------+------+---------+
-------------CASEを使ったUPDATE文----------------
ALTER TABLE USER ADD BIKOU VARCHAR(100);
UPDATE USER
SET BIKOU =
CASE
WHEN KOZUKAI <= 10000 THEN 'SUKUNAI'
WHEN (10000 < KOZUKAI AND KOZUKAI < 51000) THEN 'FUTU'
WHEN 51000 < KOZUKAI THEN 'OUI'
END;
mysql> SELECT * FROM USER ORDER BY KOZUKAI;
+----+----------+------+---------+---------+
| ID | NAME | Unit | KOZUKAI | BIKOU |
+----+----------+------+---------+---------+
| 1 | ASO | 1 | 10000 | SUKUNAI |
| 3 | YAMADA | 2 | 10000 | SUKUNAI |
| 4 | SUZUKI | 1 | 10000 | SUKUNAI |
| 5 | MIYAMOTO | 3 | 50000 | FUTU |
| 2 | KATO | 4 | 90000 | OUI |
| 6 | TANAKA | 4 | 90000 | OUI |
+----+----------+------+---------+---------+
mysql> SELECT * FROM TEST5;
+-----+---------------------+
| ID2 | TIMES2 |
+-----+---------------------+
| 1 | 2012-01-04 17:47:38 |
| 2 | 2012-01-04 17:47:38 |
| 3 | 2012-01-04 17:47:38 |
| 4 | 2012-01-04 17:47:38 |
| 5 | 2012-01-04 18:19:34 |
| 6 | 2012-01-04 18:19:34 |
| 7 | 2012-01-04 18:19:34 |
| 8 | 2012-01-04 18:19:34 |
+-----+---------------------+
mysql> SELECT * FROM TEST5 LIMIT 4 OFFSET 2;
+-----+---------------------+
| ID2 | TIMES2 |
+-----+---------------------+
| 3 | 2012-01-04 17:47:38 |
| 4 | 2012-01-04 17:47:38 |
| 5 | 2012-01-04 18:19:34 |
| 6 | 2012-01-04 18:19:34 |
+-----+---------------------+
---------------テストデータの作成------------------
mysql> CREATE TABLE USER(ID INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(20),Unit INT);
mysql> INSERT INTO USER(NAME,UNIT)VALUES('ASO',1),('KATO',4),('YAMADA',2),('SUZUKI',1),('MIYAMOTO',3),('TANAKA',4);
mysql> SELECT * FROM USER;
+----+----------+------+
| ID | NAME | Unit |
+----+----------+------+
| 1 | ASO | 1 |
| 2 | KATO | 4 |
| 3 | YAMADA | 2 |
| 4 | SUZUKI | 1 |
| 5 | MIYAMOTO | 3 |
| 6 | TANAKA | 4 |
+----+----------+------+
CREATE TABLE UNIT(ID INT AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(20));
INSERT INTO UNIT(NAME) VALUES('SOUMU'),('EIGYO'),('JIMU'),('GIJYUTSU');
mysql> SELECT * FROM UNIT;
+----+----------+
| ID | NAME |
+----+----------+
| 1 | SOUMU |
| 2 | EIGYO |
| 3 | JIMU |
| 4 | GIJYUTSU |
+----+----------+
-----グループ分けしカウント-----
mysql> SELECT UNIT.ID,UNIT.NAME ,COUNT(USER.UNIT) AS 'BUSYO_COUNT'
FROM USER
JOIN UNIT ON USER.UNIT = UNIT.ID
GROUP BY USER.UNIT;
+----+----------+-------------+
| ID | NAME | BUSYO_COUNT |
+----+----------+-------------+
| 1 | SOUMU | 2 |
| 2 | EIGYO | 1 |
| 3 | JIMU | 1 |
| 4 | GIJYUTSU | 2 |
+----+----------+-------------+
-----グループ分けした後の抽出-----
mysql> SELECT UNIT.ID,UNIT.NAME ,COUNT(USER.UNIT) AS 'BUSYO_COUNT'
-> FROM USER
-> JOIN UNIT ON USER.UNIT = UNIT.ID
-> GROUP BY USER.UNIT
-> HAVING BUSYO_COUNT >= 2;
+----+----------+-------------+
| ID | NAME | BUSYO_COUNT |
+----+----------+-------------+
| 1 | SOUMU | 2 |
| 4 | GIJYUTSU | 2 |
+----+----------+-------------+
-----------UPDATE-----------
ALTER TABLE USER ADD KOZUKAI INT;
mysql> SELECT * FROM USER;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | NULL |
| 2 | KATO | 4 | NULL |
| 3 | YAMADA | 2 | NULL |
| 4 | SUZUKI | 1 | NULL |
| 5 | MIYAMOTO | 3 | NULL |
| 6 | TANAKA | 4 | NULL |
+----+----------+------+---------+
UPDATE USER SET KOZUKAI = 10000;
mysql> SELECT * FROM USER;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | 10000 |
| 2 | KATO | 4 | 10000 |
| 3 | YAMADA | 2 | 10000 |
| 4 | SUZUKI | 1 | 10000 |
| 5 | MIYAMOTO | 3 | 10000 |
| 6 | TANAKA | 4 | 10000 |
+----+----------+------+---------+
--------条件が一致するレコードを修正---------
UPDATE USER SET KOZUKAI = 90000 WHERE UNIT = 4;
mysql> SELECT * FROM USER;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | 10000 |
| 2 | KATO | 4 | 90000 |
| 3 | YAMADA | 2 | 10000 |
| 4 | SUZUKI | 1 | 10000 |
| 5 | MIYAMOTO | 3 | 10000 |
| 6 | TANAKA | 4 | 90000 |
+----+----------+------+---------+
UPDATE USER SET KOZUKAI = 50000 WHERE UNIT = 3;
mysql> SELECT * FROM USER;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | 10000 |
| 2 | KATO | 4 | 90000 |
| 3 | YAMADA | 2 | 10000 |
| 4 | SUZUKI | 1 | 10000 |
| 5 | MIYAMOTO | 3 | 50000 |
| 6 | TANAKA | 4 | 90000 |
+----+----------+------+---------+
SELECT * FROM USER ORDER BY KOZUKAI;
+----+----------+------+---------+
| ID | NAME | Unit | KOZUKAI |
+----+----------+------+---------+
| 1 | ASO | 1 | 10000 |
| 3 | YAMADA | 2 | 10000 |
| 4 | SUZUKI | 1 | 10000 |
| 5 | MIYAMOTO | 3 | 50000 |
| 2 | KATO | 4 | 90000 |
| 6 | TANAKA | 4 | 90000 |
+----+----------+------+---------+
-------------CASEを使ったUPDATE文----------------
ALTER TABLE USER ADD BIKOU VARCHAR(100);
UPDATE USER
SET BIKOU =
CASE
WHEN KOZUKAI <= 10000 THEN 'SUKUNAI'
WHEN (10000 < KOZUKAI AND KOZUKAI < 51000) THEN 'FUTU'
WHEN 51000 < KOZUKAI THEN 'OUI'
END;
mysql> SELECT * FROM USER ORDER BY KOZUKAI;
+----+----------+------+---------+---------+
| ID | NAME | Unit | KOZUKAI | BIKOU |
+----+----------+------+---------+---------+
| 1 | ASO | 1 | 10000 | SUKUNAI |
| 3 | YAMADA | 2 | 10000 | SUKUNAI |
| 4 | SUZUKI | 1 | 10000 | SUKUNAI |
| 5 | MIYAMOTO | 3 | 50000 | FUTU |
| 2 | KATO | 4 | 90000 | OUI |
| 6 | TANAKA | 4 | 90000 | OUI |
+----+----------+------+---------+---------+
2012年1月4日水曜日
MYSQL入門 1
-------------TABLEの作成--------------
mysql> create table tb1(bang varchar(10),name varchar(10),tosi int);
-------------インサート--------------
mysql>INSERT INTO TB1 VALUES('TEST2','KAWA',11),('TEST3','TOSI',44),('TEST4','YAMA',20);
-------------テーブルのカラム構造のコピー--------------
CREATE TABLE TB1_COP LIKE TB1;
-------------テーブルのレコードのコピー--------------
INSERT INTO TB1_COP SELECT * FROM TB1;
-------------カラムデータ型を変更する--------------
mysql> ALTER TABLE tb1 mODIFY BANG varchar(200);
-------------カラムを追加--------------
mysql> ALTER TABLE tb1 ADD birthday DATETIME;
-------------主キー--------------
mysql> CREATE TABLE TB2(ID INT PRIMARY KEY, CATE VARCHAR(10));
-------------主キーオートインクリメント--------------
mysql> CREATE TABLE TB3(ID INT AUTO_INCREMENT PRIMARY KEY, CATE2 VARCHAR(10));
-------------主キーオートインクリメントにインサート--------------
mysql> INSERT INTO TB3(CATE2) VALUES('1'),('2'),('3');
-----------日付関数--------------
mysql> CREATE TABLE NOWTIME(ID INT AUTO_INCREMENT PRIMARY KEY,SHOW_TIME DATETIME);
mysql> INSERT INTO NOWTIME(SHOW_TIME) VALUES(NOW());
+----+---------------------+
| ID | SHOW_TIME |
+----+---------------------+
| 1 | 2012-01-04 16:27:11 |
| 2 | 2012-01-04 16:27:13 |
| 3 | 2012-01-04 16:27:14 |
| 4 | 2012-01-04 16:27:15 |
| 5 | 2012-01-04 16:27:16 |
+----+---------------------+
----------リミット句--------------
mysql>SELECT * FROM NOWTIME LIMIT 3;
---------WHERE句--------------
mysql> SELECT * FROM NOWTIME WHERE ID = 3;
mysql> SELECT * FROM NOWTIME WHERE ID > 2 and id < 5
---------case when--------------
SELECT ID,
CASE
WHEN ID > 2 THEN '小さい'
WHEN ID > 3 THEN 'すこし小さい'
WHEN ID > 4 THEN '中ぐらい'
WHEN ID > 5 THEN '大きい'
ELSE '図れません'
END
FROM TB3;
--------+
| 1 | 図れません
|
| 2 | 図れません
|
| 3 | 小さい
|
| 4 | 小さい
|
| 5 | 小さい
|
| 6 | 小さい
|
|
---------UPDATE--------------
mysql> UPDATE 「テーブル名」 SET「カラム名」=「設定値」;
mysql> UPDATE 「テーブル名」 SET「カラム名」=「設定値」WHERE 条件;
---------複数テーブルのレコードを合わせて表示--------------
mysql> CREATE TABLE TEST4(ID1 INT AUTO_INCREMENT PRIMARY KEY, TIMES1 DATETIME);
mysql> CREATE TABLE TEST5(ID2 INT AUTO_INCREMENT PRIMARY KEY, TIMES2 DATETIME);
mysql> INSERT INTO TEST4(TIMES1) VALUES(NOW()),(NOW()),(NOW()),(NOW());
mysql> INSERT INTO TEST5(TIMES2) VALUES(NOW()),(NOW()),(NOW()),(NOW());
mysql> (SELECT ID1 FROM TEST4)
UNION
(SELECT ID2 FROM TEST5);
+-----+
| ID1 |
+-----+
| 1 |
| 2 |
| 3 |
| 4 |
+-----+
4 rows in set (0.00 sec)
mysql> (SELECT ID1 FROM TEST4)
UNION ALL
(SELECT ID2 FROM TEST5);
+-----+
| ID1 |
+-----+
| 1 |
| 2 |
| 3 |
| 4 |
| 1 |
| 2 |
| 3 |
| 4 |
+-----+
8 rows in set (0.00 sec)
---------複数テーブルを結合して表示 内部結合 JOIN と INNER JOINはキーが一致しているレコードを取り出します--------------
SELECT * FROM TEST4
JOIN TEST5
ON TEST4.ID1 = TEST5.ID2;
+-----+---------------------+-----+---------------------+
| ID1 | TIMES1 | ID2 | TIMES2 |
+-----+---------------------+-----+---------------------+
| 1 | 2012-01-04 17:47:05 | 1 | 2012-01-04 17:47:38 |
| 2 | 2012-01-04 17:47:05 | 2 | 2012-01-04 17:47:38 |
| 3 | 2012-01-04 17:47:05 | 3 | 2012-01-04 17:47:38 |
| 4 | 2012-01-04 17:47:05 | 4 | 2012-01-04 17:47:38 |
+-----+---------------------+-----+---------------------+
SELECT * FROM TEST4
JOIN TEST5 ON ID2 > 1
WHERE TEST4.ID1 = TEST5.ID2;
+-----+---------------------+-----+---------------------+
| ID1 | TIMES1 | ID2 | TIMES2 |
+-----+---------------------+-----+---------------------+
| 2 | 2012-01-04 17:47:05 | 2 | 2012-01-04 17:47:38 |
| 3 | 2012-01-04 17:47:05 | 3 | 2012-01-04 17:47:38 |
| 4 | 2012-01-04 17:47:05 | 4 | 2012-01-04 17:47:38 |
+-----+---------------------+-----+---------------------+
3 rows in set (0.00 sec)
mysql> create table tb1(bang varchar(10),name varchar(10),tosi int);
-------------インサート--------------
mysql>INSERT INTO TB1 VALUES('TEST2','KAWA',11),('TEST3','TOSI',44),('TEST4','YAMA',20);
-------------テーブルのカラム構造のコピー--------------
CREATE TABLE TB1_COP LIKE TB1;
-------------テーブルのレコードのコピー--------------
INSERT INTO TB1_COP SELECT * FROM TB1;
-------------カラムデータ型を変更する--------------
mysql> ALTER TABLE tb1 mODIFY BANG varchar(200);
-------------カラムを追加--------------
mysql> ALTER TABLE tb1 ADD birthday DATETIME;
-------------主キー--------------
mysql> CREATE TABLE TB2(ID INT PRIMARY KEY, CATE VARCHAR(10));
-------------主キーオートインクリメント--------------
mysql> CREATE TABLE TB3(ID INT AUTO_INCREMENT PRIMARY KEY, CATE2 VARCHAR(10));
-------------主キーオートインクリメントにインサート--------------
mysql> INSERT INTO TB3(CATE2) VALUES('1'),('2'),('3');
-----------日付関数--------------
mysql> CREATE TABLE NOWTIME(ID INT AUTO_INCREMENT PRIMARY KEY,SHOW_TIME DATETIME);
mysql> INSERT INTO NOWTIME(SHOW_TIME) VALUES(NOW());
+----+---------------------+
| ID | SHOW_TIME |
+----+---------------------+
| 1 | 2012-01-04 16:27:11 |
| 2 | 2012-01-04 16:27:13 |
| 3 | 2012-01-04 16:27:14 |
| 4 | 2012-01-04 16:27:15 |
| 5 | 2012-01-04 16:27:16 |
+----+---------------------+
----------リミット句--------------
mysql>SELECT * FROM NOWTIME LIMIT 3;
---------WHERE句--------------
mysql> SELECT * FROM NOWTIME WHERE ID = 3;
mysql> SELECT * FROM NOWTIME WHERE ID > 2 and id < 5
---------case when--------------
SELECT ID,
CASE
WHEN ID > 2 THEN '小さい'
WHEN ID > 3 THEN 'すこし小さい'
WHEN ID > 4 THEN '中ぐらい'
WHEN ID > 5 THEN '大きい'
ELSE '図れません'
END
FROM TB3;
--------+
| 1 | 図れません
|
| 2 | 図れません
|
| 3 | 小さい
|
| 4 | 小さい
|
| 5 | 小さい
|
| 6 | 小さい
|
|
---------UPDATE--------------
mysql> UPDATE 「テーブル名」 SET「カラム名」=「設定値」;
mysql> UPDATE 「テーブル名」 SET「カラム名」=「設定値」WHERE 条件;
---------複数テーブルのレコードを合わせて表示--------------
mysql> CREATE TABLE TEST4(ID1 INT AUTO_INCREMENT PRIMARY KEY, TIMES1 DATETIME);
mysql> CREATE TABLE TEST5(ID2 INT AUTO_INCREMENT PRIMARY KEY, TIMES2 DATETIME);
mysql> INSERT INTO TEST4(TIMES1) VALUES(NOW()),(NOW()),(NOW()),(NOW());
mysql> INSERT INTO TEST5(TIMES2) VALUES(NOW()),(NOW()),(NOW()),(NOW());
mysql> (SELECT ID1 FROM TEST4)
UNION
(SELECT ID2 FROM TEST5);
+-----+
| ID1 |
+-----+
| 1 |
| 2 |
| 3 |
| 4 |
+-----+
4 rows in set (0.00 sec)
mysql> (SELECT ID1 FROM TEST4)
UNION ALL
(SELECT ID2 FROM TEST5);
+-----+
| ID1 |
+-----+
| 1 |
| 2 |
| 3 |
| 4 |
| 1 |
| 2 |
| 3 |
| 4 |
+-----+
8 rows in set (0.00 sec)
---------複数テーブルを結合して表示 内部結合 JOIN と INNER JOINはキーが一致しているレコードを取り出します--------------
SELECT * FROM TEST4
JOIN TEST5
ON TEST4.ID1 = TEST5.ID2;
+-----+---------------------+-----+---------------------+
| ID1 | TIMES1 | ID2 | TIMES2 |
+-----+---------------------+-----+---------------------+
| 1 | 2012-01-04 17:47:05 | 1 | 2012-01-04 17:47:38 |
| 2 | 2012-01-04 17:47:05 | 2 | 2012-01-04 17:47:38 |
| 3 | 2012-01-04 17:47:05 | 3 | 2012-01-04 17:47:38 |
| 4 | 2012-01-04 17:47:05 | 4 | 2012-01-04 17:47:38 |
+-----+---------------------+-----+---------------------+
SELECT * FROM TEST4
JOIN TEST5 ON ID2 > 1
WHERE TEST4.ID1 = TEST5.ID2;
+-----+---------------------+-----+---------------------+
| ID1 | TIMES1 | ID2 | TIMES2 |
+-----+---------------------+-----+---------------------+
| 2 | 2012-01-04 17:47:05 | 2 | 2012-01-04 17:47:38 |
| 3 | 2012-01-04 17:47:05 | 3 | 2012-01-04 17:47:38 |
| 4 | 2012-01-04 17:47:05 | 4 | 2012-01-04 17:47:38 |
+-----+---------------------+-----+---------------------+
3 rows in set (0.00 sec)
登録:
投稿 (Atom)