Mysql 日常基本使用語句

2021-08-28 02:19:40 字數 3443 閱讀 7420

目錄

1、更改資料庫表介面常用sql

2、常用查詢sql查詢函式

3、轉換函式

4、mysql設定主鍵從1開始自增

5、mysql檢查欄位中某個字元出現的次數?(檢查字段包含幾個某字元)

由於在開發過程之中經常可能涉及到針對表結構進行增加字段(需要指定在某個欄位後增加)、或者修改及重名某個字段資訊

--建立測試表

create table tabletest(

id int;

);--add支援多列,change/drop需要在每列前新增關鍵字,逗號隔開,'column'可有可無

--新增多列

alter table tabletest add (c1 char(1),c2 char(1)); --正確,add支援多列

alter table tabletest add column (c1 char(1),c2 char(1)); --正確

alter table tabletest add c1 char(1),add c2 char(1); --正確

/** 課程 一級分類 */

alter table course add course_first_category bigint(11) default 0 comment '課程一級分類id' after course_name;

--修改多列

alter table tabletest change c1 c3 char(1),change c2 c4 char(1); --正確

alter table tabletest change column c1 c3 char(1),change column c2 c4 char(1); --正確

/** 課程 調整原來的二級分類 */

alter table course change column course_category course_second_category bigint(11) default 0 comment '課程二級分類id';

alter table tabletest change (c1 c3 char(1),c2 c4 char(1)); --錯誤

--刪除多列

alter table tabletest drop c1,drop c2; --正確

alter table tabletest drop column c1,drop column c2; --正確

#日期格式化

date_format(ce.create_time,'%y-%m-%d %h:%i:%s') as createtime

date_format(task.task_start_time, '%y.%c.%d' ) as task_start_time,

#轉換檔案大小

case

when convert(ce.enclosure_size / (1024 * 1024), decimal(10, 2)) > 1024

then concat(convert(ce.enclosure_size /(1024 * 1024*1024), decimal(10, 2)), 'g')

when convert(ce.enclosure_size / (1024 * 1024), decimal(10, 2)) > 1

then concat(convert(ce.enclosure_size / (1024 * 1024), decimal(10, 2)), 'm')

else concat(convert(ce.enclosure_size / (1024), decimal(10, 2)), 'kb')

end as enclosuresize,

#計算百分比

case tresult.task_users

when 0 then concat("0.00",'%')

else concat(round(tresult.complete_task_users/tresult.task_users * 100, 2),'','%')

end complete_percentage

其中 format函式對數字型別轉化的坑 如果僅僅只是顯示字串可以使用,如果需要使用作為數字進行計算;此方式不合適

convert(param, decimal(10,2))(建議)

cast(param as decimal(10,2))(建議)

mysql 的cast()和convert()函式可用來獲取乙個型別的值,並產生另乙個型別的值。兩者具體的語法如下:

cast(value as type);  

convert(value, type);

就是cast(*** as 型別), convert(***,型別)。

mysql> select cast('3.35' as signed);  

+------------------------+

| cast('3.35' as signed) |

+------------------------+

| 3 |

+------------------------+

1 row in set

alter table user auto_increment=1; ##user是表名稱
-- 核心**

where (length(name) - length( replace (name, '.', ''))) >= 2

-- 實戰示例

mysql使用結巴語句 MySQL的使用語句

show databases 查詢所有的庫 drop database mldn 刪除庫 create database mldn 建立庫 use mldn 選擇用庫 show tables 查詢所有的表 desc 表名稱 檢視表結構 改變結構 alter table user change pas...

mysql的常用語句(基本)

1 use 切換資料庫 use 資料庫名 2 show databases 列出mysql資料庫管理系統的資料庫列表 show databases 3 show tables show tables 顯示指定資料庫的所有表,使用該命令前需要使用use命令來選擇要操作的資料庫。1 使用命令列登陸的方法...

mysql常用語句 MySQL常用語句

create table student id int primary key auto increment comment 學號 name varchar 200 comment 姓名 age int comment 年齡 comment 學生資訊 修改表注釋 alter table studen...