简体   繁体   English

如何输出最近添加到mysql数据库中的值?

[英]How to output a value that was recently added into mysql database?

I want to create a table that will output the data from mysql database that was recently added. 我想创建一个表,该表将从最近添加的mysql数据库中输出数据。

Example : 范例:

+-----+---------+-----------+---------------+-------------+
| id  | item_id | item_name | borrowed_date | expiry_date |
+-----+---------+-----------+---------------+-------------+
| B01 | N01     | book      | 12/05/2017    | 10/06/2017  |
+-----+---------+-----------+---------------+-------------+

I have tried using ORDER BY but it does not output according to newly added row. 我尝试使用ORDER BY,但是它不会根据新添加的行输出。

<?php

include"connection.php"; //contain $conn

$query = "SELECT * FROM `database` ORDER BY item_id ASC ;";
$result = mysqli_query($conn,$query);



?>

The output was not according to newly added data 输出不是根据新添加的数据

Set your id column to auto increment Give your table another name than Database. 将您的id列设置为自动递增为表指定一个不同于数据库的名称。

Then use: 然后使用:

SELECT * FROM `yourtable` ORDER BY `id` DESC LIMIT 0,1;

I suggest including one column (like a primary key) that's set to AUTO_INCREMENT . 我建议包括设置为AUTO_INCREMENT的一列(如主键)。 That way you can sort in descending order (DESC) by that ID to get the latest entry. 这样,您可以按该ID降序(DESC)排序以获得最新条目。

Something like this: 像这样:

CREATE TABLE `database` (
    id MEDIUMINT NOT NULL AUTO_INCREMENT,
    [other columns here]
    PRIMARY KEY (id)
);

And then: 接着:

SELECT * FROM `database` ORDER BY `id` DESC LIMIT 0,1;

Incidentally, I'm not sure if your table is really named "database". 顺便说一句,我不确定您的表是否真的命名为“数据库”。 If so, I suggest not using a reserved word . 如果是这样,我建议不要使用保留字 It will work since you're using backticks, but it might be a good idea to change it anyway for various reasons . 由于您正在使用反引号,因此它会起作用,但是由于各种原因 ,无论如何都要更改它可能是一个好主意。

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

相关问题 无法使用我最近在MySQL中添加的用户连接到数据库 - Can't connect to database using my recently added user in MySQL 如何获取MYSQL中最近添加的条目的关联唯一ID - how to get the associated unique ID of the recently added entry in MYSQL 如果未添加文件,如何在MySQL数据库单元中插入“空”值? - How to insert “Null” value in MySQL Database Cell if no Files are added? 使用AJAX从MySQL数据库更新PHP页面:用于计算最近添加的数据的算法 - Updating PHP page from MySQL database using AJAX: Algorithm to calculate recently added data 从MySQL数据库的所有表中检索最近添加的25个条目 - Retrieving the 25 most recently added entries from all tables in a MySQL database 使用mysql中的连接选择最近添加的数据 - selecting recently added data using joins in mysql 如何使用Facebook API获取最近添加的朋友 - How to get recently added friends with facebook api CakePHP 2.x无法更新最近添加到MySQL表的字段 - CakePHP 2.x fails to update fields recently added to a MySQL table 如果value =给定参数,则停止将条目添加到mysql数据库 - Stopping an entry being added to mysql database if the value = given parameter 如何在Codeigniter中使用输出Jason将添加的数据保存到数据库中 - How to save added data into database with output jason in codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM