#導入數據庫 [1] 一、首先建空數據庫 mysql > create database abc; 二、導入數據庫 方法一: (1)選擇數據庫 mysql > use abc; (2)設置數據庫編碼 mysql > set names utf8; (3)導入數據(注意sql文件的路徑) mysql > source /home/abc/abc.sql; 方法二: mysql -u用戶名 -p密碼 數據庫名 < 數據庫名.sql #mysql -uabc_f -p abc < abc.sql 建議使用第二種方法導入。 注意:有命令行模式,有sql命令 #根據積分查詢用戶排名 [1] select * from ( SELECT @rownum:=@rownum+1 as rownum, id, nickname FROM `wx_user`, (select @rownum:=0) t order by user_score desc ) as a where a.id = 2 #修改自增ID ALTER TABLE wx_user_test auto_increment = 40 ; #查看鏈接 show processlist; kill id #Mysql 獲取一小時前時間 select date_sub(now(), interval 1 hour); select date_sub(now(), interval -1 hour); //一小時後 select date_add(now(), interval 1 hour); //一小時後, 更多可選值參考[1] [2] #日期時間轉時間戳 [1] SELECT UNIX_TIMESTAMP( DATE_ADD( NOW( ) , INTERVAL -3 DAY ) ) ; #時間戳轉時期時間 SELECT *, FROM_UNIXTIME( created_at, '%Y-%m-%d %H:%i:%s' ) as date1 FROM `user`; #查看數據庫中全部表的記錄數 [1] select table_name,table_rows from information_schema.tables where TABLE_SCHEMA = 'test' order by table_name desc; #建立一個和已經存在表結構相同的表 CREATE TABLE wx_user1 LIKE wx_user; #重命名錶 RENAME TABLE `wx_user1` TO `wx_user` ; #查看索引 SHOW INDEX FROM wx_user; #添加多字段惟一索引 ALTER TABLE `book` ADD UNIQUE (`book_id`,`page_num`) #刪除索引 [1] ALTER TABLE wx_user DROP INDEX openid_2; #新建數據庫 CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; #查看數據庫編碼 SHOW VARIABLES LIKE '%character%' #查詢數據庫中指定表的全部字段名 [1] SELECT GROUP_CONCAT( COLUMN_NAME SEPARATOR ", " ) FROM information_schema.COLUMNS WHERE table_name = 'wx_user' AND table_schema = "database_name" #用select的數據更新另外一個表中數據 [1] UPDATE wx_user AS a INNER JOIN ( SELECT uid, COUNT( 1 ) AS read_count_total FROM wx_book_read WHERE status = 1 GROUP BY uid ) AS b ON a.id = b.uid SET a.read_count = b.read_count_total UPDATE wx_book_read_page AS a LEFT JOIN wx_book_read AS b ON a.book_id = b.book_id AND a.uid = b.uid SET a.book_read_id = b.id #查看mysql配置文件在哪 /usr/sbin/mysqld --verbose --help|grep -A 1 'Default options' #替換mysql字段中部份內容 UPDATE wx_user SET image = REPLACE(image , 'http://', 'https://') WHERE id = 1; #mysql字符串拼接 UPDATE `goods_cat` SET name = CONCAT( name, '2' ) WHERE TYPE =2; #mysql字符串處理函數 LENGTH(TRIM(url)), LENGTH(url) CHAR_LENGTH #mysql按in中內容排序 [1] SELECT id, title, description, TYPE FROM wx_article WHERE id IN ( 784, 743, 596, 659, 685, 617, 836, 815, 813, 774 ) ORDER BY INSTR( ',784,743,596,659,685,617,836,815,813,774,', CONCAT( ',', id, ',' ) ) SELECT id, title, description, FIELD( id, 3, 5, 1, 4, 2 ) as order_data FROM wx_article WHERE ID IN ( 3, 5, 1, 4, 2 ) ORDER BY FIELD( id, 3, 5, 1, 4, 2 ) SELECT FIELD( 1, 3, 5, 1, 4, 2 );//3 #隨機閱讀次數100-200次 select @i:=100;select @j:=200; UPDATE `wx_article_test` SET `view_count` = FLOOR(@i + rand() * (@j - @i + 1)) WHERE 1;