mysql 學習筆記 day05
阿新 • • 發佈:2019-02-15
/* insert into join_teacher values(4, '孫武', 'male'); insert into join_class values(4,'php0505','202'); 連結查詢 先將所有資料鏈接起來再進行查詢; 內連線,外連線,自然連結 */ --------------------------------------內連線------------------------------------ -- 資料內部的內連線 連結的多個數據必須存在 -- mysql 中預設是 inner join 可以寫成 join -- on 跟連結條件 也可以用 where table_left inner join table_right on ; -- 比較標準的語法 on 連結條件, where 過濾條件 select t.t_name, tc.begin_date, tc.days from join_teacher t inner join join_teacher_class tc on t.id = tc.t_id where days > 15; select t.t_name, tc.begin_date, tc.days from join_teacher t cross join join_teacher_class tc; select id , days, t_name from join_teacher inner join join_teacher_class using (id); --------------------------------------外連線------------------------------------ -- 如果負責連結的一個或多個數據不存在 外連線 可以有連結不到的資料 -- 外連線不能用where作為連結條件,但可以使用on 和 using select t.t_name, tc.begin_date, tc.days from join_teacher t left outer join join_teacher_class tc on t.id = tc.t_id; -- 當欄位重名時寫表名 -- 左外連線 -- 在連線時,如果出現左邊表,資料鏈接不到右邊表的情況,則左邊表資料保留, -- 如果出現右邊表的資料左邊表沒有,右邊表的資料丟棄 -- outer join outer 可以省略 select t.id as t_id, tc.id as tc_id, t.t_name, tc.begin_date, tc.days from join_teacher t left join join_teacher_class tc on t.id = tc.t_id; -- 右外連線 select t.id as t_id, tc.id as tc_id, t.t_name, tc.begin_date, tc.days from join_teacher t right join join_teacher_class tc on t.id = tc.t_id; --------------------------------------自然連結----------------------------------------------- /* 通過mysql自己的判斷選用連線型別 內:natural join 外:left natural join right natural join 連線時支援多表同時連線 */ /* outfile 操作 dumpfile */ -- 設定資料格式 -- fields terminated -- lines terminated select * from teacher; select * into outfile 'e:/one.txt' from teacher; select * into outfile 'e:/one.txt' fields terminated by ',' from teacher select * from dumpfile 'e:oe' from teacher; -----------------------------------插入資料------------------------------------------------------ insert into table_name(column_name) values(value); insert into table_name set column_name1=value2, column_name2=value2; insert into table_name(column_name, column_name2) values (value1,value2), (value1,value2); -- 插入資料時主鍵衝突 --預設報錯,表示不會插入 -- 可以insert插入 on duplicate key update 不加set; insert into table_name(id, t_name)values(1,'ji') on duplicate key update t_name='ji'; -- 用select結果作為插入資料 insert into table_name1 (column_name1, column_name2) select column_name1,column_name2 from table_name2; -- replace 主鍵不衝突直接插入,否則替換 replace into teacher values(1,'han','dd', 90); alter table table_name drop primary key;-- 如果主鍵自動增長,執行不成功 -- load data infile; 在匯出時將主鍵刪除掉,匯入時再新建主鍵 -----------------------------------刪除資料------------------------------------------------------ --delete 允許使用limit 限制刪除的條數 limit 配合order by 使用 delete from table_name order by days limit 10; delete from one, two using one join two on one.public_field = two.public_field; -- 清空表 truncate -- 類似 delete from table -----------------------------------更新資料------------------------------------------------------ update one join two on one_public_field = two_public_field set one_data=value where id = value; ------------------------------------檢視操作-------------------------------------------------------- create table info_teacher( id int primary key auto_increment, t_name varchar(20), salary decimal(10,2) ); insert into info_teacher values (null, '韓信',180.90), (null, '李白', 230.65), (null, '韓飛', 159.03); create view v_teacher as select id, t_name from info_teacher; insert into v_teacher values(null, '江湖'); +----+--------+ | id | t_name | +----+--------+ | 1 | 韓信 | | 2 | 李白 | | 3 | 韓飛 | | 4 | 江湖 | +----+--------+ 4 rows in set (0.00 sec) mysql> select * from info_t +----+--------+--------+ | id | t_name | salary | +----+--------+--------+ | 1 | 韓信 | 180.90 | | 2 | 李白 | 230.65 | | 3 | 韓飛 | 159.03 | | 4 | 江湖 | NULL | +----+--------+--------+ 4 rows in set (0.00 sec) /* 檢視是動態的,只儲存了一條sql語句;2014/3/11 drop view v_teacher; */ drop view if exists v_teacher; alter view v_name as select * from info_teacher;