简体   繁体   English

MySQL替换行,其中字段a等于x并且字段b等于y

[英]MySQL REPLACE rows WHERE field a equals x and field b equals y

I'm trying to construct a 'new comments' feature where the user is shown the number of new comments since the last visit. 我正在尝试构建“新评论”功能,向用户显示自上次访问以来新评论的数量。 I've constructed a 'Views' table that contains the topic id, the user id and a timestamp. 我构建了一个包含主题ID,用户ID和时间戳的“视图”表。 What's the best way of updating the timestamp every time the user visits that topic or inserting a fresh row if one doesn't exist? 每次用户访问该主题时更新时间戳或插入不存在的新行的最佳方法是什么? At the moment I have: 目前,我有:

REPLACE INTO Views (MemberID, TopicID, Visited)
SET ('$_SESSION[SESS_MEMBER_ID]', '$threadid', '$t')
WHERE MemberID = $_SESSION[SESS_MEMBER_ID] AND TopicID = $threadid

However, this does not work. 但是,这不起作用。 Is it possible to do this in one query? 是否可以在一个查询中执行此操作?

Try replacing SET with VALUES . 尝试将SET替换为VALUES The syntax is: 语法为:

REPLACE [LOW_PRIORITY | DELAYED]
  [INTO] tbl_name [(col_name,...)]
  {VALUES | VALUE} ({expr | DEFAULT},...),(...),...

Edit: Note that this only really works if you have set MemberID and TopicID as a unique key in your table. 编辑:请注意,这仅在您将MemberIDTopicID设置为表中的唯一键时才真正起作用。 If you do this, then you should just be able to do either: 如果执行此操作,则应该可以执行以下任一操作:

REPLACE INTO Views (MemberID, TopicID, Visited)
VALUES ('$_SESSION[SESS_MEMBER_ID]', '$threadid', '$t')

or 要么

INSERT INTO Views (MemberID, TopicID, Visited)
VALUES ('$_SESSION[SESS_MEMBER_ID]', '$threadid', '$t')
ON DUPLICATE KEY UPDATE Visited = '$t'

(of course, you should be using proper placeholders so Little Bobby Tables doesn't make a visit and destroy your database) (当然,您应该使用适当的占位符,以便Little Bobby Tables不会访问并破坏您的数据库)

I can't think of a way with only one query, but you could try this: 我想不出只有一个查询的方法,但是您可以尝试以下方法:

<?php
$where = "where `MemberID`=".$_SESSION['SESS_MEMBER_ID']." and `TopicID`=".$threadid;
if( mysql_fetch_assoc( mysql_query( "select * from `Views` ".$where)))
   mysql_query( "update `Views` set `Visited`='".$t."' ".$where);
else
   mysql_query( "insert into `Views` (`MemberID`, `TopicID`, `Visited`) values ('".$_SESSION['SESS_MEMBER_ID']."', '".$threadid."', '".$t."')");
?>

That would run two queries total. 这将总共运行两个查询。

If you're using the TIMESTAMP data type, it will update itself automatically every time the row is altered. 如果您使用的是TIMESTAMP数据类型,则每次更改行时它都会自动更新。 So just call update on the row (if it exists, otherwise create it) and change just the topicId. 因此,只需在该行上调用update(如果存在,否则创建它)并仅更改topicId。 The database will handle changing the time. 数据库将处理更改时间。

Really, I wouldn't use REPLACE. 真的,我不会使用REPLACE。 Instead, maybe you could create a stored procedure that checks to see if the row exists (just pass the memberId and topicId) and creates it if not, and then returns the timestamp to you. 相反,也许您可​​以创建一个存储过程,以检查该行是否存在(只需传递memberId和topicId),如果不存在则创建它,然后将时间戳返回给您。 This makes more sense. 这更有意义。

And using a stored proc means only one trip to the DB. 而且使用存储的proc意味着仅一次数据库访问。

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

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