(個人總結更新,mysql)常用sql語句介紹:
①博主習慣小寫;②test_table:資料庫表;③column_name:字段;④str:字串等型別資料
1、建立庫表
create table test_table (colnum_name1 varchar(50),colnum_name2 datatime)
2、刪除庫表
drop table test_table
3、select 查詢字段資料:
select * from test_table
select column_name1,column_name2 from test_table
4、where 篩選資料:
select * from test_table where column_name='str'
select * from test_table where column_name!='str'
5、篩選為空/不為空的記錄:
為空的記錄:select * from test_table where column_name is null
不為空的記錄:select * from test_table where column_name is not null
6、delete 刪除資料記錄:
delete from test_table where column_name='str'
7、insert 增加資料記錄:
insert into test_table (column_name1,column_name2) values('str1','str2')
8、update 修改字段資料:
update test_table set column_name1='str1' where column_name2='str2'
update test_table set column_name1='str1',column_name2='str2' where column_name3='str3'
9、replace 替換字段資料中某個字串:
update test_table set column_name=replace(column_name,'str1','str2')
10、join 資料庫表關聯:
select a.*,b.* from test_table1 left join test_table2 on table1.column_name1=table2.column_name2
select a.*,b.* from test_table1 right join test_table2 on table1.column_name1=table2.column_name2
select a.*,b.* from test_table1 inner join test_table2 on table1.column_name1=table2.column_name2
11、and/or/in條件查詢:
交叉條件(and):select * from test_table where column_name1='str1' and column_name2='str2'
並列條件(or):select * from test_table where column_name1='str1' and column_name2='str2'
多條件(in):select * from test_table where column_name in('str1','str2')
12、distinct 字段資料去重:
select distinct column_name from test_table
13、group by 按欄位資料分組:
select * from test_table group by column_name
分組求和:select c_type,sum(c_total) as c_total from test_table group by c_type
分組計數:select c_type,count(*) as c_cnt from test_table group by c_type
分組計數(字段資料不為空):select count(column_name1) as column_name1from test_table group by column_name2
14、order by 按欄位資料排序:
select * from test_table order by column_name(順序/公升序)
select * from test_table order by column_name desc(逆序/降序)
優先順序問題: where 優先於 group by,group by 優先於 order by
15、獲取日期(下列時間都可用於where條件,最後又乙個where條件演示):
按年月日查詢的:
當前日期:select date(now())
當前年月:select left(now(),7)
當前月份:select month(now())
當前年份:select year(now())
當前日期前7天:select date_add(now(),interval -7 day)
當前日期前30天:select date_add(now(),interval -30 day)
當前日期前乙個月(詳細時間):select date_add(now(),interval-1 month)
當前日期前乙個月(年月日):select date(date_add(now(),interval-1 month))
當前日期前乙個月(年月):select left(date_add(now(),interval-1 month),7)
當前日期前乙個月(月份):select month(date_add(now(),interval-1 month))
當前日期前乙個月(月份):select month(date_add(now(),interval-1 month))
當前日期三個月資料:
select c_id,c_time from test_table where c_time date(date_add(now(),interval-3 month))
日期比較(日期加減):
日期比較(大-小):select datediff(date(now()),mid("dqsc_20190714002754",6,8))
按年月查詢的:
引數為年月,如「2019-06」,
按博主自己測試的總結,sql的年月增減是按照完整的年月日來執行的,如果需要按年月查詢時,需要將年月改為年月日的模式(在年月後再拼接日期如「-01」),才可以進行年月的增減,以下是乙個例子,其他場景大家可以根據自己的情況做延伸。
select * from (select '5月資料' as name,'2019-05' as sj union all
select '6月資料' as name,'2019-06' as sj union all
select '7月資料' as name,'2019-07' as sj) as test
where sj=left(date(date_add('$-01',interval-1 month)),7)
16、帶引數的sql查詢:
預設:select * from test_table
引數名:para
①等於條件查詢:select * from test_table where column_name ='$'
②引數為空查詢所有:select * from test_table where 1=1 $
③ in條件查詢:select * from test_table where column_name in ('$')
④ in條件為空查詢所有:select * from test_table where 1=1 $
17、length 長度
select column_name,len(column_name) as str_len from test_table
select * from test_table where len(column_name)>7
18、group_concat 分組後組內資料拼接
select group_concat(c_province,c_city) from tablename group by c_province,c_city
分組後組內資料排序拼接:
select group_concat(c_province,c_city order by c_province) from tablename group by c_province,c_city
select group_concat(c_province,c_city order by c_province desc) from tablename group by c_province,c_city
19、索引:
建立索引:create index id from tablename(c_id)
建立多欄位索引:create index id from tablename(c_id,c_name)
檢視索引:show index from tablename
刪除索引:drop index id on tablename
20、檢視版本資訊
show variables like '%version%'
MySql 常用SQL語句
create database kali use kali show tables create table students sno varchar 10 primary key,sname varchar 10 not null,varchar 2 check in 男 女 age varcha...
常用sql語句(mysql)
給表新增列 sql alter table table name add column col name varchar 255 not null default default value 增加表列,指定格式 sql alter table table name add col name bool...
MySQL常用SQL語句
查詢一張表中的所有資料 sql view plain copy select from table name 查詢一張表中的指定列資料 sql view plain copy select column a,column b from table name 按條件查詢一張表中的所有資料 sql vi...