laravel php的if判断,php – Laravel if if语句

我从DB中选择了问题.

基本上我想要实现的是:

我有一个有3列的表

type | number | date

我需要做基于列(类型)的地方

If(type = 1) then where number > 1 else where date today()

因此,如果type等于1,则将数字应用于数字,否则应用于何处.有可能这样想吗? Laravel Eloquent可以做到吗?

谢谢.

原始查询和数据类型是:

type = string

number =整数

date = timestamp(Y-m-d H:i:s)

询问

SELECT * FROM table_name

何时类型=天数日期>现在()ELSE数字> 0 END

表模式

CREATE TABLE横幅(

id bigint(20)unsigned NOT NULL AUTO_INCREMENT,

type varchar(255)COLLATE utf8_unicode_ci NOT NULL DEFAULT’0′,

number int(10)unsigned NOT NULL DEFAULT’0′,

finish_at timestamp NOT NULL DEFAULT’0000- 00:00:00′,

PRIMARY KEY(id)

)ENGINE = InnoDB AUTO_INCREMENT = 12 DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci

有效的解决方案:

select * from `banners`

where `banners`.`block_id` = 1 and `banners`.`block_id` is not null

and IF (`type` = 'days', `finish_at` > now(), `number` > 0)

Laravel方式:

ParentModel::banners()->whereRaw("IF (`type` = 'days', `finish_at` > now(), `number` > 0)")->get();

解决方法:

你可以像使用它一样使用它

SELECT * FROM tablename WHERE IF (`type` = 1, `number` > 1,`date` = today())

使用Laravel Eloquent,您可以像使用它一样使用它

ModelName::whereRaw('IF (`type` = 1, `number` > 1,`date` = today())')->get();

标签:php,mysql,laravel,eloquent

来源: https://codeday.me/bug/20190609/1202643.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值