cfsky 发表于 2018-10-22 12:29:53

mysql 常用sql语句

  增加字段:
  altertable表Xaddprovince_id int(11)   COMMENT'省份id' AFTER province DEFAULT 1;
  删除字段:
  ALTER TABLE 表X DROP COLUMN 字段X
  修改字段类型:
  ALTER TABLE 表X   ALTER COLUMN字段X新类型X
  删除数据:
  delete from 表X where borough_id=832
  更新数据:
  1、更新内容从同表另一字段获取:
  update 表X set keyword=title where keyword is null or keyword='';
  2、更新内容从关联表某字段获取:
  update 表X p setmember_id=
  (selecta.member_idfrom 表Ya WHEREp.member_id = a.id) wheremember_id < 9352
  3、更新一字段,取同表另外两字段之和
  update 表X set dialogue_id=(consignee+sender)
  4、将字段置为空
  update 表Xset real_total = NUll where sell_type = 'cash_on_delivery'
  5、替换某字段数据中指定的部分字符串
  update 表X set content=
  replace(content,'src="/images','src="http://xxx.xxx.xx/images')
  where content like '%src="/images%'
  6、从一字段中截取部分字符串更新到另一字段:
  update 表X set picture=SUBSTRING_INDEX(big_picture, ';', 1) ;
  函数注释:截取第一个“;”左侧的内容
  7、如果数据后面有多余的逗号,则清除掉
  UPDATE shop_product SET keyword=
  LEFT(keyword,char_length( keyword) - 1)WHERERIGHT( keyword,1 )=','
  函数注释:
  char_length:返回字符串所占的字符数,不管汉字还是数字或者是字母都算是一个字符
  LEFT(str,n):返回字符串str的最左面n个字符。
  RIGHT(str,n):返回字符串str的最右面n个字符。
  8.数据库之间的关联查询和更新
  #shop表和另一数据库关联查询,查询shop表id为1的记录;
  select * from shop,xxx.society_join sj where shop.join_sq_id=sj.id and shop.id=1;
  #更新shop表的省份,数据来自另一数据库
  update shop set shop.province=(select sj.province from xxx.society_join sj where   shop.join_sq_id=sj.id ) where shop.id=1
  9.获取字段中的部分内容并更新到另一个字段;{使用函数SUBSTRING_INDEX();也可以配合函            数SUBSTRING()实现更复杂的匹配}
  X、函数测试:SELECT SUBSTRING_INDEX('www;mysqlcom', ';', 1); #获取;前面的字段串
  A、查询测试:SELECT client_index_picture, SUBSTRING_INDEX(client_big_picture, ';',   1),client_big_picture from product where product.client_big_picture like '%;%' limit 10 ;
  B、批量更新:update product set client_index_picture=SUBSTRING_INDEX(client_big_picture, ';', 1) ;
  分组统计后,按统计结果排序
  select s.*from(select orders.shop_id,orders.shop_name,shop.shop_owner_email, count(*) as num from ordersleft join shop on shop.shop_id=orders.shop_id where orders.pay_time is not null group by orders.shop_id ) sorder by num desclimit 100

页: [1]
查看完整版本: mysql 常用sql语句