MySQL:仅在第一个查询未返回结果时才执行第二个查询 [英] MySQL: execute the second query only if the first query doesn't return result

查看:54
本文介绍了MySQL:仅在第一个查询未返回结果时才执行第二个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下查询:

SELECT * FROM best WHERE best = 'value' AND lang = 'x' 
   UNION 
SELECT * FROM best WHERE best = 'value' AND lang = 'custom' 
LIMIT 1

基本上,我只需要一条记录,其中best ='value'和lang = 'x',如果找不到该记录,则需要使用lang = 'custom'

Basically I just need one record where best = 'value' and lang = 'x', if this record isn't found then I need to execute the second query with lang = 'custom'

MySQL是否足够聪明,以至于考虑到存在LIMIT 1,当联合的第一个查询返回某些内容时他不需要执行第二个查询?

Is MySQL smart enough to understand that considering there is the LIMIT 1, when the first query of union returns something he doens't need to execute the second query?

只有一个查询我可以做:

To have just one query I could do:

SELECT * FROM best WHERE best = 'value' AND lang IN ('x','custom') LIMIT 1

但是对于这个查询,我不能说给lang = 'x'

But with this query I can't say give more priority to record with lang = 'x'

推荐答案

您可以使用这将解决您的优先级"问题:)

This will take care of your "priority" problem :)

这篇关于MySQL:仅在第一个查询未返回结果时才执行第二个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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