是否可以首先从DB中选择,然后如果某些内容为真,则在同一查询中插入到db中?
以下是我现在的处理方式:
$sql = mysql_query("SELECT * FROM ".$prefix."_active_users WHERE userid = $playerid AND DATE(timestamp) = '$sel_tag'");
$row = mysql_fetch_assoc($sql);
if(!$row){
mysql_query("INSERT INTO ".$prefix."_active_users (userid,timestamp) VALUES ($playerid,'$now')");
}
希望得到帮助和感谢; - )
答案 0 :(得分:4)
在我阅读问题时,如果没有行,则需要插入。
所以,在一个查询......
mysql_query("
INSERT INTO ".$prefix."_active_users (userid,timestamp)
SELECT $playerid, '$now'
FROM dual
WHERE NOT EXISTS (SELECT * FROM ".$prefix."_active_users
WHERE userid = $playerid AND DATE(timestamp) = '$sel_tag')
");
这与其他答案INSERT.. SELECT
答案 1 :(得分:1)
insert into YourTable
(col1, col2, ...)
select col1, col2, ...
where not exists
(
select *
from YourTabe
where col1 = 42
)