MySQL常用SQL語句(一)

2021-10-02 04:38:00 字數 1747 閱讀 4796

mysql 按照指定欄位的指定值排序:

select * from t_user order by field(id, 0, 2, 11, 1, 3, 4, 5, 6, 8, 9, 10) asc
使用find_in_set(str,sbstr)函式查詢 「,」 號(逗號)隔開的資料;

filed 字段儲存形式為多個逗號隔開的值:1,2,3,5,6

select * from t_user  where find_in_set('1',filed)
修改時間的年月,其他保留不變

select t.created_at,addtime (concat("2014-02-",dayofmonth(t.created_at))+interval 0 hour,time(t.created_at)) as newtime

from t_user t where t.id=1

-- 檢查表,獲得不是ok就是有問題需要修復。

check table t_user;

-- 修復表

repair table `t_user`;

網際網路公司為啥不使用mysql分割槽表:

alter table t_logs partition by range(id)  

(partition p0 values less than (3000000),

partition p1 values less than (6000000),

partition p2 values less than (9000000),

partition p3 values less than (12000000),

partition p4 values less than maxvalue);

通過日期 計算是第幾個星期

select weekofyear(adddate('2012-01-29',1));-- 從星期天開始算第一天

select weekofyear(now());-- 從星期一開始算第一天(其中:now() 為計算當前日期的函式)

通過另外乙個表查詢結果來更新表 update from

update b_gem gem inner join b_productlist list set gem.status='new',gem.storeroomid=list.storeroom where list.type=1 and list.sid=2 and list.serid=gem.serid
將查詢結果中的2個字段放到合併到乙個字段顯示

select concat(first_name,' ',last_name) as customername from t_agmt_customer
show full fields from t_user;
-- 將選擇 information_schema 資料庫 

select concat(round(sum(index_length)/(1024*1024), 2), ' mb') as 'total index size' from tables where table_schema = 'mytable_test' and table_name='t_user';

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...