简体   繁体   English

检索具有特定值的数据库表中的最新条目

[英]Retrieving the most recent entry in a database table with a certain value

I have a table with three fields - userID, couponID, last_couponID.我有一个包含三个字段的表 - userID、couponID、last_couponID。

When the user accesses a coupon, I run this query:当用户访问优惠券时,我运行以下查询:

  mysql_query("INSERT INTO users_coupons (userID, couponID) VALUES ('$recordUserID', '$recordCoupID')");

Further, I have another query that should insert the last couponID into the field last_couponID, by polling the database table and finding the most recent result for the current userID.此外,我还有另一个查询应该通过轮询数据库表并查找当前 userID 的最新结果,将最后一个 couponID 插入字段 last_couponID。

I believe it is as such:我相信它是这样的:

 SELECT couponID FROM users_coupons ORDER BY userID LIMIT 1

Am I correct?我对么? Or should I use a different query?或者我应该使用不同的查询?

Like this:像这样:

userID   couponID
  1         3
  1        13
  1        23
  2         5
  2         3

So if I wanted userID 2's latest coupon, it'd be '3', and for userID 1, it'd be '23'.因此,如果我想要 userID 2 的最新优惠券,它会是“3”,而对于 userID 1,它会是“23”。 I just want the last entry in the database table where the userID matches a value I provide.我只想要数据库表中 userID 与我提供的值匹配的最后一个条目。

I would just add a primary key (autoincrementing) to the users_coupons table.我只需在 users_coupons 表中添加一个主键(自动递增)。

When you want the latest coupon of a user,在想要用户的最新优惠券的时候,
SELECT couponID FROM users_coupons WHERE userID =? ORDER BY id DESC LIMIT 1

Assuming that couponID is numeric, and the last couponID for a certain userId is the greatest (as a number), you can do:假设 couponID 是数字,并且某个 userId 的最后一个 couponID 是最大的(作为数字),您可以这样做:

SELECT MAX(couponID) AS lastCouponId FROM users_coupons WHERE userId = <put here the user id>

EDIT: since you've edited your question, I edit my answer.编辑:既然你已经编辑了你的问题,我编辑我的答案。

You have to add an auto-increment primary key, otherwise you can't know exactly which entry is the last one.您必须添加一个自增主键,否则您无法确切知道哪个条目是最后一个。 When I answered, I supposed the last coupon for a certain userId was the one with the greatest couponId .当我回答时,我认为某个userId的最后一张优惠券是具有最大优惠券的优惠couponId Are you sure you can't just make things work this way?你确定你不能让事情这样运作吗?

Something along the lines of...类似于...的东西

SELECT couponID
FROM users_coupons
WHERE userID = <current user id>
ORDER BY <primary key of user_coupons table if it's an identity column> DESC
LIMIT 1

...is more appropriate. ……更合适。 Your query as it stands doesn't do anything with the 'current' user ID.您的查询对“当前”用户 ID 没有任何作用。

If you are actually after the highest couponID then SELECT MAX(couponID)... as suggested by Giacomo is a good idea - coupled with a check for the UserID matching the current user ID.如果您实际上是在最高的 couponID 之后,那么SELECT MAX(couponID)...正如 Giacomo 所建议的那样是一个好主意 - 再加上检查与当前用户 ID 匹配的 UserID。

@Giacomo's answer is valid if you are incrementing the CouponID reliably as an identifier.如果您将 CouponID 作为标识符可靠地递增,@Giacomo 的答案是有效的。 If you have merged in data or are adjusting this value any other way then it may not be correct.如果您已合并数据或正在以任何其他方式调整此值,则它可能不正确。

In theory, if you consider CouponID to be a surrogate key then you cannot use it to explicitly determine insert order.理论上,如果您将 CouponID 视为代理键,那么您不能使用它来明确确定插入顺序。 If you intend for it to be used for the purpose of insert order then you also need to make sure your supporting code and DB maintenance plans promote this use.如果您打算将其用于插入订单,那么您还需要确保您的支持代码和数据库维护计划促进这种使用。

I contend that the "correct" method is to also store a DateTime我认为“正确”的方法是也存储一个DateTime

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

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