---
title: "MySQL 实用技巧篇"
author: "jontan"
date: "2014-01-10T21:45:17Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2014-01-17T11:21:50Z"
id: 2002510
url: "https://summary.iteye.com/blog/2002510"
---

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:

<!--THE END-->

- 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:

<!--THE END-->

- 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';