简体   繁体   English

如何使用MySQL查询查找表中最后插入的varchar值

[英]How to find last inserted varchar value in a table using MySQL query

I have a table student , table structure is follow 我有一个桌子学生 ,桌子结构如下

student_id | name | class|
-------------------------
2-12-2013 | test | 3    |
-------------------------
2-13-2013 |test2 | 5    |
-------------------------

in this table i want add new record, its student_id become 2-14-2013 . 在此表中,我要添加新记录,其student_id变为2-14-2013 For that i want to get the last inserted student_id. 为此,我想获取最后插入的student_id。 how can i get it by MySQL query . 我如何通过MySQL查询获取它。 Am alredy use mysql_insert_id() But its not working, How to solve this issue? 可以使用mysql_insert_id()但它不起作用,如何解决此问题?

Define Your student table like below:- 如下定义您的学生表:-

student_id | add_date  | name | class|
--------------------------------------
1          | 2013-12-2 | test | 3    |
--------------------------------------
2          | 2013-13-2 |test2 | 5    |
--------------------------------------

student_id would be int auto increamented primary key student_id将是int自动无提示主键

then execute Query like below to get last inserted id:- 然后执行如下查询以获取最后插入的ID:-

select student_id from student order by student_id desc limit 1;

Hi You has to set primary key to your table as integer auto-increment then you can use this last inserted id 嗨,您必须将表的主键设置为整数自动增量,然后才能使用此最后插入的ID

$con = mysql_connect("localhost", "root", "", "chirag2");
$sql = "INSERT INTO test_student ( student_id,name,class)values('3-12-2013','test1','3' )";
$res = mysql_query($con, $sql);

echo mysql_insert_id($con);
die;

Note: you need to truncate table before alter it and set new primary key to it so take backup before this 注意:您需要在修改表之前截断表并为其设置新的主键,因此在此之前进行备份

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM