怎么用mysql创建一个id自动编号,文本类型的title和content,日期型的times的

怎么用mysql创建一个id自动编号,文本类型的title和content,日期型的times的,第1张

CREATE TABLE `表名` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`title` text,

`times` datetime DEFAULT NULL,

PRIMARY KEY (`id`)

)

mysql>select * from testgroup

+------+---------------------+

| id | time|

+------+---------------------+

|1 | 2012-02-28 01:20:44 |

|2 | 2012-02-28 01:20:47 |

|3 | 2012-02-28 01:20:50 |

| 53 | 2012-02-28 01:21:08 |

| 95 | 2012-02-28 01:21:09 |

| 18 | 2012-02-28 01:21:09 |

|4 | 2012-02-28 01:21:10 |

| 65 | 2012-02-28 01:21:10 |

| 15 | 2012-02-28 01:21:11 |

| 79 | 2012-02-28 01:21:12 |

| 51 | 2012-02-28 01:21:12 |

| 17 | 2012-02-28 01:21:12 |

|1 | 2012-02-28 01:21:26 |

|2 | 2012-02-28 01:21:30 |

|3 | 2012-02-28 01:21:36 |

|3 | 2012-02-28 01:21:38 |

+------+---------------------+

16 rows in set (0.00 sec)

mysql>select * from testgroup t1 where (t1.id,t1.time) in(select t2.id,max(t2.time) from testgroup t2 group by t2.id)

+------+---------------------+

| id | time|

+------+---------------------+

| 53 | 2012-02-28 01:21:08 |

| 95 | 2012-02-28 01:21:09 |

| 18 | 2012-02-28 01:21:09 |

|4 | 2012-02-28 01:21:10 |

| 65 | 2012-02-28 01:21:10 |

| 15 | 2012-02-28 01:21:11 |

| 79 | 2012-02-28 01:21:12 |

| 51 | 2012-02-28 01:21:12 |

| 17 | 2012-02-28 01:21:12 |

|1 | 2012-02-28 01:21:26 |

|2 | 2012-02-28 01:21:30 |

|3 | 2012-02-28 01:21:38 |

+------+---------------------+

12 rows in set (0.04 sec)

----------------------------------------风骚的分割线--------------------------------------------

我这边time字段是datetime类型你可以先将你time字段做类型转换处理,str_to_date(str,'%Y-%m-%d')即可


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8640778.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存