連接配接相關操作:
#mysql -uroot -p
#show databases;
#use XXX_database;
#show tables;
建表語句:
create table 表名(列名稱,列類型 [列屬性][預設值]),
engine 引擎名 charset 字元集
增:
往那張表增,增哪幾列,各為什麼值;
insert into 表名 (列1,列2, ... 列n)
values
(值1, 值2.......值N)
如果不聲明拆入的列,則預設拆入所有列;
改:update
修改哪張表,修改哪幾列,修改成什麼值?
在哪幾行上生效
update 表名
set
列1 = 值1
列2 = 值2
.。。
列N = 值N
where 表達式
delete:
删除哪幾張表的資料,删那些行
delete from 表名
查:
select * from 表名
查詢5種子句:where 後面的表示式子代入到每行,确認是否成立;
where shop_price - market_price > 200;
in(值1, 值2, 值3, ...值N)等于值1-N任意之一,都可以;
select good_id, cat_id from goods where cat_id in (4, 5);
between 在某一範圍内;
between 值1 and 值2, 表示在值1和值2之間(允許等于邊界);
select good_it, cat_id from goods where cat_id between 1 and 6;
or用法
select good_id, good_name, shop_price from goods where shop_price >=3000
and shop_price <=5000 or shop_price >=500 and shop_price<=1000;
not的用法:
select good_id, cat_id from goods where cat_id not in (4, 5);
select good_id, cat_id from goods where cat_id!=4 and cat_id != 5;
模糊查詢:‘ % ’:通配任意字元 '_':單個字元
select good_id, cat_id from goods where good_name like '%諾基亞%';
group by (要和聚合函數(統計函數)一起使用)
作用:把行 按 字段 分組
文法:group by col1, col2, ... colN
運用場合:
常見于統計場合,如按欄目計數文章數,
統計每個人的平均成績等;
max(shop_price) //聚合函數
select good_id, good_name, max(shop_price) from goods; //文法是錯誤的
select min(shop_price) from goods; //ok
select cat_id, max(shop_price) from goods group by cat_id;
select min(shop_price) from goods;
select min(goods_id) from goods;
select sum(goods_number) from goods;
select avg(shop_price) from goods;
計算表中函數
select count(*) from goods;
select cat_id, min(shop_price) from goods group by cat_id; //ok
select cat_id, count(*) from goods group by cat_id;
select good_id, good_name, market_price-shop_price from goods;
select cat_id, sum(shop_price *good_number) from goods group by cat_id;
給列取别名: as
select cat_id, sum(shop_price*goods_number) as huokuan from goods group by cat_id;
select test1.id, test1.name, test1.address, test2.age from test1, test2 where test2.age=test1.id;
等價于:(使用内連接配接)
select test1.id, test1.name, test1.address, test2.age from test1 inner join test2 on test1.id=test2.age;
inner join(内連接配接):
inner join:如果表中有至少一個比對,則傳回行;
left join:左連接配接,即使右表中沒有比對,也從左表傳回所有的行;
select test1.id, test2.name, test1.address, test2.age from test1 left join test2 on
test1.id=test2.age;
(右表: test2, 左表:test1)
right join: 右連接配接,即使左表中沒有比對,也從右表傳回所有的行;
select test1.id, test1.name, test1.address, test2.age from test1 right join test2 on
test1.id = test2.age;
(右表:test2 左表:test1)
full join: 隻要其中一個表中存在比對,就傳回行;
union:聯合統計結果
select name from test1 where age between 25 and 50 union select name from test2 where age<100;