create table users (pkid int auto_increment primary key,...)
表创建完成后设置递增ID:
alter table users add pkid int auto_increment primary key
很多时候不希望pkId从1开始,我们可能希望他从10000开始:
alter table users AUTO_INCREMENT=10000
4
你也可以修改现有的递增值, 比如大批量删除数据后,想id从654321退回123456开始:
alter table users AUTO_INCREMENT=123456
使用模拟的 ROW_NUMBER + PARTITION 的方式SELECT
sale_item,
sale_date,
sale_money
FROM
(
SELECT
CASE
WHEN @cn != sale_item THEN @rownum:= 1
ELSE @rownum:= @rownum + 1
END AS No,
@cn := sale_item AS sale_item,
sale_date,
sale_money
FROM
(SELECT @rownum:=0) r,
(SELECT @cn:='') p,
SALE_REPORT
ORDER BY
SALE_ITEM,
SALE_DATE DESC
) subQuery
WHERE
no <=3
+-----------+---------------------+------------+
| sale_item | sale_date | sale_money |
+-----------+---------------------+------------+
| A | 2012-12-31 00:00:00 | 2012.00 |
| A | 2012-12-30 00:00:00 | 2012.00 |
| A | 2012-12-29 00:00:00 | 2012.00 |
| B | 2012-12-31 00:00:00 | 12.00 |
| B | 2012-12-30 00:00:00 | 12.00 |
| B | 2012-12-29 00:00:00 | 12.00 |
| C | 2012-12-31 00:00:00 | 31.00 |
| C | 2012-12-30 00:00:00 | 30.00 |
| C | 2012-12-29 00:00:00 | 29.00 |
+-----------+---------------------+------------+
9 rows in set (0.02 sec)
SHOW CREATE TABLE table_name;如:SHOW CREATE TABLE user得出结果:| user | CREATE TABLE `user` ( `pkid` int(8) NOT NULL, `username` varchar(24) NOT NULL COMMENT `password` varchar(24) NOT NULL COMMENT `name` varchar(36) NOT NULL COMMENT `state` varchar(12) NOT NULL COMMENT `lv` int(8) NOT NULL default '1' COMMENT ' `beizhu` varchar(255) default NULL COMMENT PRIMARY KEY (`pkid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 这里就是字符集了。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)