Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在Laravel中查询构建Where IF条件_Mysql_Laravel_Laravel 5.2 - Fatal编程技术网

Mysql 如何在Laravel中查询构建Where IF条件

Mysql 如何在Laravel中查询构建Where IF条件,mysql,laravel,laravel-5.2,Mysql,Laravel,Laravel 5.2,我正在MySQL编辑器中构建一个查询,但现在我想在Laravel5中的查询生成器SQL中转换下面的SQL,有人能帮我吗?我在拉雷维尔搜索了很多关于IF条件的地方,但没有找到 select * from eventos where inicio = '2016-09-06 09:41' and recorrencia = 0 or recorrencia = 1 and ( if (recorrencia_tipo = 'semanal', recorrencia_intervalo,

我正在MySQL编辑器中构建一个查询,但现在我想在Laravel5中的查询生成器SQL中转换下面的SQL,有人能帮我吗?我在拉雷维尔搜索了很多关于IF条件的地方,但没有找到

select *
from eventos
where inicio = '2016-09-06 09:41'
and recorrencia = 0
or recorrencia = 1 
and 
(
    if (recorrencia_tipo = 'semanal', recorrencia_intervalo, 7) LIKE concat('%', weekday('2016-09-06 00:00:00'), '%')
        or
    if(recorrencia_tipo = 'mensal', recorrencia_intervalo, 0) = day('2016-09-06 09:41')
        or
    (
        if (recorrencia_tipo = 'anual', substring_index(recorrencia_intervalo, '/', 1), 0) = DAY('2016-09-06 09:41')
            or 
        if (recorrencia_tipo = 'anual', substring_index(recorrencia_intervalo, '/', -1), 0) = MONTH('2016-09-06 09:41')
    )
)
我在拉雷维尔一直建到现在

Evento::where('inicio', '2016-09-06 09:41')
            ->where('recorrencia', 0)
            ->orWhere('recorrencia', 1)
            ->where('recorrencia_tipo', function ($query) {
            })->get();

我不知道这是否是正确的方法,但我使用了@vijaykuma所说的whereRaw

按照查询进行操作

$eventos = Evento::where('inicio', $data)
            ->where('recorrencia', 0)
            ->orWhere('recorrencia', 1)
            ->whereRaw("
                IF (recorrencia_tipo = 'semanal', recorrencia_intervalo, 7) LIKE concat('%', weekday('{$data}'), '%')
                    OR
                IF (recorrencia_tipo = 'mensal', recorrencia_intervalo, 0) = DAY('{$data}')
                    OR  
                (
                    IF (recorrencia_tipo = 'anual', substring_index(recorrencia_intervalo, '/', 1), 0) = DAY('{$data}')
                        AND
                    IF (recorrencia_tipo = 'anual', substring_index(recorrencia_intervalo, '/', -1), 0) = MONTH('{$data}')
                )
            ")->get();
试试这个:

Evento::where('inicio', '2016-09-06 09:41')
        ->where('recorrencia', 0)
        ->orWhere('recorrencia', 1)
        ->where('recorrencia_tipo', function ($query) {
          if(condition){
            $query->select('column')->from('table')->where('where clause');
          }
          else{
            $query->select('column')->from('table')->where('where clause');
          }
        })
        ->get();

希望它有帮助=)

如果您在这里登陆,试图使If作为SELECT的一部分工作,这可能会有帮助:

$query = DB::table('mytable')
    ->select(DB::raw('id,title,IF(some_column = 1, some_column,null) as some_column'))
    ->get();
请记住:

IF(expr1、expr2、expr3)

如果expr1为TRUE,则If()返回expr2。否则,它将返回expr3


你为什么不试一下原始查询呢?谢谢,伙计,你的回答对我帮助很大