mysql where if test,IF ...在WHERE子句MySQL中的ELSE

I'm trying to use a IF...ELSE function in a MySQL WHERE statement without success.

I have this query:

SELECT id

FROM mytable

WHERE restrcountry NOT LIKE '%*nl*%'

AND (IF languages LIKE '%*nl*%', 1, 0) = 1;

Ok, this is my query with IF statement.

However, how can I use also "ELSE"?

Example, I would like to do something similar:

IF language match nl ---> select id field where language is nl

ELSE IF language NOT match nl ---> select id field where language is en

How can I do this in a MySQL query, please?

Thanks to all!

解决方案

The syntax for IF is :

IF(test_expr, then_expr, else_expr)

so you could do something like IF(test1, result1, IF(test2, result2, else_result)) but it would not be very readable, so there's the CASE expression for that purpose.

CASE WHEN test1 THEN result1

WHEN test2 THEN result2

ELSE else_result END

If you want to condition a select column, you can use the IF in the select fields directly:

SELECT IF(match, nl_column en_column) AS lang

FROM table

Note that an expression in a where clause is either TRUE or FALSE, so writing

IF(expr, TRUE, FALSE)

is the same as

expr

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值