標籤:style ar os sp strong on bs ad 時間
mysql主鍵操作
刪除表主鍵:
alter table student drop primary key;
增加表主鍵:
alter table student add primary key(id);
重新設定主鍵自增(前提是需要先執行刪除表主鍵語句):
alter table student modify id bigint(20) auto_increment primary key;
驗證是否重名
加上limit 1 就是告訴mysql 找到即刻返回,不浪費後面的時間
select count(*) from student where name = ‘tom‘ limit 1;
group by語句:按條件分成不同的組
建立customer表
| id |
name |
cost |
|
| 1 |
tom |
1000 |
|
| 2 |
jojnny |
2000 |
|
| 3 |
tom |
350 |
|
| 4 |
bruce |
730 |
|
| 5 |
mark |
3000 |
|
| 6 |
ethan |
2300 |
|
| 7 |
bruce |
1740 |
|
查詢每個客戶花費的總額:
select name, sum(cost) from customer group by customer;
查詢的結果為:
| name |
sum(cost) |
| bruce |
2470 |
| ethan |
2300 |
| johnny |
2000 |
| mark |
3000 |
| tom |
1350 |
order by語句:按條件順序排列
按花費從小到大查詢
select * from customer order by cost;
查詢結果為
| id |
customer |
cost |
| 3 |
tom |
350 |
| 4 |
bruce |
730 |
| 1 |
tom |
1000 |
| 7 |
bruce |
1740 |
| 2 |
johnny |
2000 |
| 6 |
ethan |
2300 |
| 5 |
mark |
3000 |
group by order by組合:按分組條件分組後,再按排序條件排序
按客戶總花費從小到大排列
select name, sum(cost) from customer group by customer order by sum(cost);
查詢結果為:
| name |
sum(cost) |
| tom |
1350 |
| johnny |
2000 |
| ethan |
2300 |
| bruce |
2470 |
| mark |
3000 |
distinct語句:去除重複項
查詢出所有的客戶名字
select distinct name from customer;
查詢結果為:
| name |
| tom |
| johnny |
| bruce |
| mark |
| ethan |
mysql SQL文法總結