mysql 常用命令

來源:互聯網
上載者:User

標籤:

--擷取目前時間 前一天的時間 interaval 後面參數是幾 代表前幾天date(DATE_SUB(NOW(),INTERVAL 1 DAY);--  轉碼CAST(countreg as char)CAST(CONCAT(‘"+download+"‘,adid) as char) as apk-- 不改變現有數組的情況下  串連兩個或多個數組 返回 組成之後新的數組CONCAT()CONCAT(CONVERT(a.adid,char))-- mysql case 語句case when then  when then else end--   查詢為數位列select * from visitlog where LENGTH(0+adid)=LENGTH(adid)-- 查詢前一天的資料select * from visitorlogs where date(firsttime)=date(DATE_ADD(now(),INTERVAL -1 day));--   查詢重複資料select user_name,count(*) as count from user_table group by user_name having count>1; --  dos環境下中文亂碼set character_set_results=gb2312;--如果 expr1 不是 NULL,IFNULL() 返回 expr1,否則它返回 expr2。IFNULL(expr1,expr2) --  截取字串   1. 要截取的字串 2. 截取條件  3. 出現的位置  正數 從左往右截  負數  從右往左截substring_index (‘要截取的字串‘,‘.‘,2);--當插入資料時,如出現錯誤時,如重複資料,將不返回錯誤,只以警告形式返回。--所以使用ignore請確保語句本身沒有問題,否則也會被忽略掉insert ignore into--  匯出資料庫mysqldump -u(username) -p(password) (dbname) > (路徑加檔案名稱).sql-- 將表一資料匯入到表二中   兩張表的欄位要一致 insert into table2 (‘欄位1‘,‘欄位2‘) select 欄位1 , 欄位2 from table1 ;--  如果資料存在 執行更新操作 如果不存在執行插入REPLACE INTO ext (extid,noid,key1,value1) VALUES +values+ ;insert  into visitlog (imei ,imsi, plat, version,visitdate ,jspname,adid,count) values "+ " (?,?,?,?,?,?,?,1) on  "+ " duplicate key update count=count+1--  設定許可權GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘ pwd‘ WITH GRANT OPTION;flush privileges;--  分組後顯示每組的幾條資料select a.fdid , a.adid , a.apk , a.pkgName , a.name from(select t1.*,(select count(*)+1 from recommend where fdid=t1.fdid and rid<t1.rid) as group_id from recommend t1) awhere a.group_id<=5-- 分組查詢 顯示日期最小的資料select idfa,devicetoken,idfv,dcode , min(regdate) as date from romuse GROUP BY idfa,devicetoken,idfv,dcode order by date(regdate)delete from iappleapp where  type=2  and id  not in ( select id from ( select max(id) as id , count(1) as count  from iappleapp  where type=2 group by appname  ORDER BY count desc) as b );--  將本地txt檔案 以命令的形式 儲存到資料庫中mysql -uroot -p20110725 lock[資料庫名稱] -e "load data local infile ‘d:/zfy4.txt‘ into table pm_clientlog_w6(表明)  fields terminated by ‘|‘  (imei,imsi,regdate,jspfile,packageid,plat,version)"-- 重複資料刪除資料delete from game  where id in (select id from (select  max(id) as id,count(title) as count from game group by title having count >1 order by count desc) as tab )alter table [biaoming] add [ziduanming]  [leixing](length)alter table isapp modify column appname varchar(50) ;-- 擷取當前日期select curdate();        -- 擷取當月最後一天。            select last_day(curdate());-- 擷取本月第一天                   select DATE_ADD(curdate(),interval -day(curdate())+1 day);    -- 擷取下個月的第一天select date_add(curdate()-day(curdate())+1,interval 1 month);-- 擷取當前月的天數select DATEDIFF(date_add(curdate()-day(curdate())+1,interval 1 month ),DATE_ADD(curdate(),interval -day(curdate())+1 day)) from dual; -- 將查詢結果儲存到本地txt檔案/usr/local/mysql/bin/mysql -h romherom3.mysql.rds.aliyuncs.com -uuser_romhe -ppasswd_20110725 rom3 -e "select concat(imei,‘,‘),null,concat(‘,‘,imsi,‘,‘),null,concat(‘,‘,plat,‘,‘),null,concat(‘,‘,regdate) from rom_user where date(regdate)>=date(‘2014-10-01‘) and soid >=‘200188003000868‘ and soid<=‘200188003001087‘ ">/home/webs/meng.txtmysql -h rdsk5j61yfna0312q977.mysql.rds.aliyuncs.com -uappstoreljd -pljd20151123 appstore -e "select idfa from iosrecimeisad where adid=‘876336838‘">/alidata1/linshi/zfy/ximalaya.txt/usr/local/mysql/bin/mysql -hrds32m2q332m2q3.mysql.rds.aliyuncs.com -uappstore -pdelong20110725 appstore -e "select * from iosuser_old">/home/webs/zfy/iosold.txt-- 查詢指定資料庫有多少張表 appstore 為要查詢的資料庫SELECT count(*) TABLES, table_schema FROM information_schema.TABLES  where table_schema = ‘appstore‘ GROUP BY table_schema;ALTER TABLE `iosappcheck` ADD COLUMN `registerdate`  varchar(255) NULL COMMENT ‘蘋果帳號註冊時間‘;--刪除計劃任務DROP EVENT  e_test_insert (計劃任務名稱)--當然前提是這個事件存在,否則會產生ERROR 1513 (HY000): Unknown event錯誤,因此最好加上IF EXISTS--DROP EVENT IF EXISTS e_test;--停止計劃任務ALTER EVENT    e_test_insert (計劃任務名稱) DISABLE;--開始事件ALTER EVENT e_test ENABLE(計劃任務名稱);1. hash索引尋找資料基本上能一次定位元據,當然有大量碰撞的話效能也會下降。而btree索引就得在節點上挨著尋找了,很明顯在資料精確尋找方面hash索引的效率是要高於btree的; 2. 那麼不精確尋找呢,也很明顯,因為hash演算法是基於等值計算的,所以對於“like”等範圍尋找hash索引無效,不支援; 3. 對於btree支援的聯合索引的最優首碼,hash也是無法支援的,聯合索引中的欄位要麼全用要麼全不用; 4. hash不支援索引排序,索引值和計算出來的hash值大小並不一定一致。-- MySQL 添加列,修改列,刪除列ALTER TABLE:添加,修改,刪除表的列,約束等表的定義。查看列:desc [表名];修改表名:alter table [t_book] rename to [bbb];添加列:alter table 表名 add column 列名 varchar(30);刪除列:alter table 表名 drop column 列名;修改列名MySQL: alter table bbb change nnnnn hh int;修改列名SQLServer:exec sp_rename‘t_student.name‘,‘nn‘,‘column‘;修改列名Oracle:lter table bbb rename column nnnnn to hh int;修改列屬性:alter table t_book modify name varchar(22);sp_rename:SQLServer 內建的預存程序,用與修改表的定義。--MySQL 查看約束,添加約束,刪除約束 添加列,修改列,刪除列查看錶的欄位資訊:desc 表名;查看錶的所有資訊:show create table 表名;添加主鍵約束:alter table 表名 add constraint 主鍵 (形如:PK_表名) primary key 表名(主鍵欄位);添加外鍵約束:alter table 從表 add constraint 外鍵(形如:FK_從表_主表) foreign key 從表(外鍵欄位) references 主表(主鍵欄位);刪除主鍵約束:alter table 表名 drop primary key;刪除外鍵約束:alter table 表名 drop foreign key 外鍵(區分大小寫);修改表名:alter table t_book rename to bbb;添加列:alter table 表名 add column 列名 varchar(30);刪除列:alter table 表名 drop column 列名;修改列名MySQL: alter table bbb change nnnnn hh int;修改列名SQLServer:exec sp_rename‘t_student.name‘,‘nn‘,‘column‘;修改列名Oracle:alter table bbb rename column nnnnn to hh int;修改列屬性:alter table t_book modify name varchar(22);sp_rename:SQLServer 內建的預存程序,用與修改表的定義。  

  

mysql 常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.