在同一查询中选择并更新 [英] Select and Update in same query

查看:61
本文介绍了在同一查询中选择并更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在同一查询中进行选择和更新吗?我正在尝试通过侧边栏的视图来计算观看次数,以创建热门博客文章.

Can you select and update in the same query? I'm trying to count views to create a top blog posts by views for my sidebar.

$sql = mysql_query("SELECT * FROM articles WHERE id='$id' LIMIT 1
                    UPDATE pages SET views=views+1 WHERE ID=$id");

推荐答案

不,您不能这样做,但是执行两个查询没有错.

No, you cannot do that, but there is nothing wrong with doing two queries.

       mysql_query("UPDATE pages SET views=views+1 WHERE ID=$id");
$sql = mysql_query("SELECT * FROM articles WHERE id=$id");

此外,如果id是您不必在此处做的主键,则id是唯一的,因此它将始终只有一个符合您条件的结果.

Also, if id is the primary key you don't have to do LIMIT 1 here, id is unique, therefore it will always have only one result matching your condition.

这篇关于在同一查询中选择并更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆