7

I came to a problem with select from DB. Basically what I am trying to achieve is: I have a table with 3 columns

type | number | date

I need to do where based on column (type)

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

So if the type is equal to 1 then apply where to number else apply where to date. It is possible to do such a thing? And it is possible to do it with Laravel Eloquent? Thank you.

The original query and datatype are: type = string number = integer date = timestamp (Y-m-d H:i:s)

Query

SELECT * FROM table_name
WHERE CASE WHEN type = days THEN date > now() ELSE number > 0 END

Table schema

CREATE TABLE banners (
  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:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Solution that worked:

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 Way:

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

4 Answers 4

8

You can use it like as

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

Using Laravel Eloquent you can use it like as

ModelName::whereRaw('IF (`type` = 1, `number` > 1,`date` = today())')->get();
Sign up to request clarification or add additional context in comments.

13 Comments

You have an error in your SQL syntax; WHERE IF (type = days, finish_at > now() ,left > 0)' at line 3
What is days over here ? You didn't specified it within your question
This is my data, type is a string, if accepts only integers?
Please specify your conditions clearly. What you want to achieve and what variables you're using over here also post the original error that you were getting over here
Updated my question. Thank you.
|
4

Try this:

$query = SomeModel::where(function($query){
                            $query->where(function($query){
                                 $query->where('type',1)->where('number',1)
                             })
                            ->orWhere(function($query){
                                 $query->where('type',1)->where('date', today())
                             })
                         })

We use orWhere to solve this problem. And need a where wraps outside of orWhere to make it clear with another condition if need.

If you are using MySQL, try DB::raw and use CASE in WHERE:

Laravel Eloquent Select CASE?

4 Comments

Good solution But I cant use OR I need exact where
What DBMS are you using?
@PoWeR if you are using MySQL, just take a look at DB raw queries . Using Case in where may help. stackoverflow.com/questions/17092112/…
MySQL I will take a look
3

You should use case for this,

SELECT * FROM tablename
WHERE  CASE WHEN type = 1 THEN number > 1 ELSE date = today() END 

2 Comments

This is what I need but MySQL gives error You have an error in your SQL syntax; WHERE CASE WHEN type=days THEN finish_at > 1 ELSE left > 0 END
@PoWeR will have a check and will update the answer.
2
$date = '';//Date you want
$number = ''//Number you want
$field = ''//date or number
SomeModel::where($field, '=', $value)->where('type', '=', 'sometype');

You can use Eloquent of Laravel this way, its not necessary to do method chaining in single line.

EDIT: On the first i didnt got the question right.Now its fixed assume you will have dynamic "type" put it into use statement too.

EDIT 2: You can represent the field as literal according to if condition and then pass it to the where clause.

1 Comment

From where did you got variable $type? I think you didn't got the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.