博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql 语句总结
阅读量:5011 次
发布时间:2019-06-12

本文共 3236 字,大约阅读时间需要 10 分钟。

1.多表查询

  SELECT a.id,catid,thumb,title FROM v9_gamedown as a LEFT JOIN v9_gamedown_data as b ON a.id=b.id WHERE a.status=99 and b.artist="'.$artist.'" limit 0,'.$num

2.把一张表中的字段移动到另一张表

  update v9_gamedown_data as a set a.n_language = (select language from v9_gamedown as b where b.id = a.id)

  update v9_gamedown_data as a set a.n_thumb2 = (select thumb2 from v9_gamedown as b where b.id = a.id)

3.字符串替换

  UPDATE `v9_record_data` SET `attach2` = REPLACE(`attach2`, 'http://www.fengzigame.com/uploadfile/2014/0326/', 'http://files.fengzigame.com/cd/share/') WHERE `id` > 0;
  UPDATE `v9_record_data` SET `attach` = REPLACE(`attach`, 'http://www.fengzigame.com/uploadfile/', 'http://files.fengzigame.com/cd/pro/') WHERE `id` > 0;

 

4.把一张表的字段复制到另外一张表中

  insert into apple_id (`apple_id`,`password`,`UDID`)  select `apple_id`,`password`,`UDID` from appleaccount where UDID is not NULL;

5.建表

CREATE TABLE IF NOT EXISTS `v9_guan_history` (  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,  `tagid` int(10) DEFAULT '0' COMMENT 'tagid',  `email` VARCHAR(32) NOT NULL DEFAULT '' COMMENT 'appleid',  `idfa` VARCHAR(64) DEFAULT '' COMMENT '设备码',  `status` tinyint(1) unsigned  DEFAULT 0 COMMENT '类型',  `activedate` VARCHAR(12)  DEFAULT '' COMMENT '激活时间',  `installdate` VARCHAR(12)  DEFAULT '' COMMENT '装机时间',  PRIMARY KEY (`id`),  KEY `mykey` (`tagid`,`installdate`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 6.删除字段

  ALTER TABLE `haha2` DROP `name`

7.添加字段

  ALTER TABLE `v9_fx_history` ADD `devid` int(11) DEFAULT 0 AFTER `uid` ;

8.修改字段名称

  ALTER TABLE `v9_dxt_history` CHANGE `madein` `area` varchar(16) DEFAULT ''

9.修改字段类型

  ALTER TABLE `haha2` CHANGE `num` `num` INT( 11 ) NULL DEFAULT '0'

10.修改表名

  ALTER TABLE `haha2` RENAME `haha` ;

11.修改字段的位置

  ALTER TABLE tb_dept1 MODIFY column1 VARCHAR(12) AFTER location;

12.修改表的存储引擎

  ALTER TABLE tb_dev ENGINE=MyISAM;

11.

  复制旧表的数据到新表(假设两个表结构一样)

  INSERT INTO 新表 SELECT * FROM 旧表

  复制旧表的数据到新表(假设两个表结构不一样)

  INSERT INTO 新表(字段1,字段2,…….) SELECT 字段1,字段2,…… FROM 旧表

12.多表更新

  UPDATE v9_qd_dev dev, v9_qd_account user SET dev.storename = user.storename  WHERE dev.storeid = user.id ;

13.添加索引

  ALTER TABLE book ADD INDEX mykey(id,name,num);

  ALTER TABLE book ADD UNIQUE INDEX uniquekey (tagid);

14.查看表中的索引

  SHOW INDEX FROM book \G;

15.删除索引

  ALTER TABLE book DROP INDEX mykey;

  DROP INDEX mykey ON book;

 16.三张关联表的查询

select v9_app_enjoy.id,v9_app_enjoy.gid,title,tagid,attach4 from v9_app_enjoy left join v9_gamedown2 on v9_app_enjoy.gid = v9_gamedown2.id left join v9_gamedown2_data on v9_gamedown2_data.id=v9_gamedown2.id;

 

select title,tagid,attach4 from v9_app_enjoy as a left join v9_gamedown2 as b on a.gid = b.id left join v9_gamedown2_data as c on c.id=b.id  where a.uid in (5634,11211) ORDER BY a.id desc limit 2;

 17. 多表分组更新

update v9_qd_account as u,(select sum(money) as money, storeid from v9_qd_mingxi where inputtime>0 group by storeid) as m set now_money= now_money+money where u.id = m.storeid

 18.关联删除

delete from v9_gamedown2  where status = 0  delete v9_gamedown2_data from v9_gamedown2_data LEFT JOIN v9_gamedown2 ON v9_gamedown2_data.id=v9_gamedown2.id WHERE v9_gamedown2.id IS NULL

 20.在一个字段前添加字符串

  update aa set name=concat('x',name)

转载于:https://www.cnblogs.com/mr-amazing/p/3954345.html

你可能感兴趣的文章
黑寡妇黄飞鸿
查看>>
leetcode 217 Contains Duplicate 数组中是否有重复的数字
查看>>
The Ctrl & CapsLock `problem'
查看>>
MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
查看>>
linux故障判断
查看>>
Leetcode 23. Merge k Sorted Lists(python)
查看>>
Java进阶知识点6:并发容器背后的设计理念 - 锁分段、写时复制和弱一致性
查看>>
Makefile ===> Makefile 快速学习
查看>>
face detection[HR]
查看>>
java性能调优工具
查看>>
C# 其他的Url 文件的路径转化为二进制流
查看>>
cmake使用
查看>>
ios7上隐藏status bar
查看>>
构造方法和全局变量的关系
查看>>
python3基础05(有关日期的使用1)
查看>>
ArrayList的使用方法
查看>>
面向对象高级
查看>>
Bitwise And Queries
查看>>
打印Ibatis最终的SQL语句
查看>>
HBase之八--(3):Hbase 布隆过滤器BloomFilter介绍
查看>>