MySQL:如果确定值,则更新列 [英] MySQL: Update a columns if certain value

查看:116
本文介绍了MySQL:如果确定值,则更新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是SQL专家,快速的Google搜索并不能给我带来太多好处(尽管我认为我没有使用最好的搜索词组).希望你们能给我一些指导.

I'm not an SQL expert and a quick google search hasn't gotten me much (though I don't think I'm using the best search phrases). Hope you guys can give me some guidance.

我在存储数字的表中有一列.我希望仅将此数字设置为2(如果以前等于1).我想知道是否有可能避免这种情况:

I have a certain column in a table that stores a number. I want this number to be set to 2 ONLY if it previously equaled 1. I was wondering if it was possible to avoid this:

$val = mysql_fetch_row(mysql_query("SELECT columnA FROM tableName WHERE something = '$someValue'));

if($val[0] == 1) { .....UPDATE tableName SET columnA = 2....... }

是否可以直接查询数据库?像这样:SET columnA = 2 if columnA = 1

Is it possible to directly query the database? With something like this: SET columnA = 2 if columnA = 1

推荐答案

实际上,您要查找的查询应该是:

Actually the query you're looking for should be:

update tableName set columnA = 2
where columnA = 1

您提到的"if"实际上是一个条件.这些条件是在update语句的where子句中设置的.

The "if" you're mentioning is actually a condition. And these conditions are set in the where clause of the update statement.

就这样:)

这篇关于MySQL:如果确定值,则更新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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