MySQL 实用技巧篇

 
MySQL float类型查询

字段 ID float(7,4)

当ID=3.0000时我可以select * from table where ID=3或者select * from table where ID=3.0000查出来

而当ID=3.1000时用select * from table where ID=3.1 或者select * from table where ID=3.1000都查不出来

此时解决方法有2种:
1.将float改为double类型,不会出现这种问题.但是如果数据库中数据量庞大,或者修改量太大,则不适合这个方法.这个方法只适合设计数据库的初期阶段.


2.设置float的精度然后进行查询就可以了.

    select * from table where format(ID,4) =format(3.1000,4)

注意:精度不能超过6.否则出错.因为float类型最多允许精确到小数点后6位.
MySQL 的 RowNum 实现

MySQL 下面没有RowNum,排序后序号却无法得到,比较麻烦!

在网上找了再三,通过比较,确认了以下的方法是可行的 :

SELECT @rownum:=@rownum+1 rownum, CollectSn From
(SELECT @rownum:=0,bbgmain.* FROM qbdb.bbgmain WHERE collectsn!='' ORDER BY collectsn limit 10) t
  1. table comments:
  •    create 
   CREATE TABLE `table1` (
  `field1` VARCHAR(20) COLLATE utf8_general_ci NOT NULL DEFAULT '',
  `field2` INTEGER(11) DEFAULT NULL COMMENT 'aaa',
  PRIMARY KEY (`field1`) 
)ENGINE=InnoDB
CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'
COMMENT='AAAA;  
  •    query
SHOW CREATE TABLE table1;
select * from information_schema.tables where table_name='TABLE1';
  •     modify
  ALTER TABLE table1 COMMENT = 'new  comment';
 
  1. table field comments:
  •    create 
   CREATE TABLE `table1` (
  `field1` VARCHAR(20) COLLATE utf8_general_ci NOT NULL DEFAULT '',
  `field2` INTEGER(11) DEFAULT NULL COMMENT 'aaa',
  PRIMARY KEY (`field1`) 
)ENGINE=InnoDB
CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'
COMMENT='AAAA;  
  •    query
show full fields from tableA;
select column_name, column_comment from information_schema.columns where table_name='table1';
 
  •     modify
 ALTER TABLE table1 MODIFY COLUMN field1 varchar(20) not null COMMENT  'field new Commonts';
 

猜你喜欢

转载自summary.iteye.com/blog/2002510