简体   繁体   English

如何在mysql中选择最新的条目?

[英]How do I select the most recent entry in mysql?

i want to select the most recent entry from a table and see if that entry is exactly the same as the one the user is trying to enter. 我想从表中选择最近的条目,看看该条目是否与用户尝试输入的条目完全相同。 How do I do a query to "select * from the most recent entry of 'posting'"? 如何查询“从最近的'发布'条目中选择*”?

    $query="Select * FROM 
        //confused here 
 (SELECT * FROM posting ORDER BY date_added DESC)
 WHERE user_id='{$_SESSION['user_id']}'
 AND title='$title'
 AND price='$price'
 AND city='$city'
 AND state='$state'
 AND detail='$detail'
 ";

 $data = mysqli_query($dbc, $query);
 $row = mysqli_fetch_array($data);
 if(mysqli_num_rows($data)>0)
 {
  echo "You already posted this ad. Most likely caused by refreshing too many times.";
  echo "<br>";
  $linkposting_id=$row['posting_id'];
  echo "See the <a href='ad.php?posting_id=$linkposting_id'>Ad</a>";
 }
 else
 {
        ...insert into the dbc
        }

//would this query work? or how do i use it to select the last ID in the table 'posting'?
    $query="SELECT LAST_INSERT_ID(posting)
     WHERE user_id='{$_SESSION['user_id']}'
     AND title='$title'
     AND price='$price'
     AND city='$city'
     AND state='$state'
     AND detail='$detail'
     ";

The orderby piece needs to come at the end of your query. orderby片段需要在查询结束时出现。 So the query you're asking for is something like this: 所以你要求的查询是这样的:

select * from posting where .... order by date_added desc limit 1;

Single-use form tokens can help prevent duplicate submissions, too. 一次性表单令牌也可以帮助防止重复提交。

Based on your comments, either of the following queries will retrieve the single latest record: 根据您的评论,以下任一查询都将检索单个最新记录:

SELECT * FROM posting ORDER BY date_added DESC LIMIT 1

SELECT * FROM posting ORDER BY posting_id DESC LIMIT 1

You would usually have a unique identifier in your table and give that column an auto_increment . 您通常在表中有一个唯一标识符,并为该列提供auto_increment It's possible you already have such an identifier. 你可能已经有了这样的标识符。

You can then fetch the latest record's ID using PHP's mysql_insert_id() or mySQL's LAST_INSERT_ID() . 然后,您可以使用PHP的mysql_insert_id()或mySQL的LAST_INSERT_ID()获取最新记录的ID。

The latter would be preferable because according to the manual, it keeps the count per connection , so other processes or web pages can't screw up the count in between. 后者是优选的,因为根据手册,它保持每个连接的计数,因此其他进程或网页不能搞砸其间的计数。 It's not entirely sure from the PHP manual whether the PHP function does the same. 从PHP手册中可以确定PHP函数是否也是如此。

I suggest creating a unique key on your table instead of doing what you described. 我建议在你的桌子上创建一个独特的键,而不是你所描述的。 This way you avoid duplicated content independent of how many users are using your application concurrently. 这样,您可以避免重复内容,而无需同时使用您的应用程序的用户数量。 Your solution would fail if A inserts something, B inserts something else, then A submits again. 如果A插入一些东西,B插入其他东西,然后A再次提交,你的解决方案将失败。

Example of adding a unique key to an existing table: 向现有表添加唯一键的示例:

ALTER TABLE posting add UNIQUE KEY uk_posting (title, price, city)

Replace title, price, city with the fields combination that must be unique. title, price, city替换title, price, city必须唯一的字段组合。 Then all you have to do is to handle the error upon insert. 然后你要做的就是在插入时处理错误。

使用LIMIT 1将查询限制为最后一个(最近的)条目

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

相关问题 我如何使HTML选择框默认为最新的数据库条目 - How do I make html select box default to most recent db entry MYSQL查询最近的条目 - MYSQL Query most recent entry 如何从每个组中选择一个最新的? (尝试选择每个人的最新消息,并使其链接到对话) - How do I select one most recent of each group? (Trying to select the most recent message from each person and make it link to the conversation) 如何根据用户ID在一个表中获取时间戳的最新条目 - How do I get the most recent entry on a timestamp in one table based on a user ID MySQL查询最新条目 - Mysql query most recent entry with a twist 在PHP中,如何要求mySQL数据库检索与某个值匹配的最新记录? - In PHP, how do I ask a mySQL database to retrieve the most recent record that matches a certain value? 我如何简单地使用mysql检索wordpress中的最新发布帖子? - How do I simply retrieve the last most recent publish post in wordpress with mysql? 在PHP中,如何要求mySQL数据库检索多个与某个值匹配的最新记录? - In PHP, how do I ask a mySQL database to retrieve multiple most recent records that match a certain value? 如何使用PHP和MySQL在文本区域中显示两个最新帖子? - How do I display the two most recent posts in a textarea using PHP and MySQL? 在mySQL中选择具有不同ID的最新条目 - Select the most recent entries with different IDs in mySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM