MySQL if语句子查询值 [英] MySQL if statement subquery value

查看:1335
本文介绍了MySQL if语句子查询值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

SELECT IF((SELECT something FROM table) AS tmp) > 0, tmp, (SELECT bla FROM oter_table))

不幸的是'as tmp'和tmp的回归不管用。我怎样才能让它发挥作用?不重复查询:

Unfortunately the 'as tmp' and returing the tmp is not working. How can I get this to work? without doing repeating the query:

SELECT IF((SELECT something FROM table) > 0, (SELECT something FROM table), (SELECT bla FROM oter_table))


推荐答案

你可以用< a href =http://dev.mysql.com/doc/refman/5.0/en/user-variables.html =nofollow> 用户定义的变量

SELECT IF(
          @val:=(SELECT something FROM table1) > 0, 
          @val, 
          (SELECT bla FROM table2)
       )

SQL小提琴

这篇关于MySQL if语句子查询值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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