How to use AND and OR in MySQL query? - mysql

I want to get those records whose date_last_copied field is empty or less than the current date. I tried this, but it did not give me the desired result:
$tasks = $this->Control->query("
SELECT *
FROM
`controls`
WHERE
`owner_id` = ".$user_id."
AND `control_frequency_id` = ".CONTROL_FREQUENCY_DAILY."
OR `date_last_copied` = ''
OR `date_last_copied` < ". strtotime(Date('Y-m-d'))."
");

Current query looks something like this, I think. That is, find the records with the correct owner_id and frequency_id, where the date_last_copied is null or less than a certain date. Is that logic correct?
SELECT *
FROM controls
WHERE owner_id = ::owner_id::
AND control_frequency_id = ::frequency_id::
AND (
date_last_copied IS NULL
OR date_last_copied < ::date::
)
But we should really be using the CakePHP query builder, rather than running raw SQL. This article gives some details. If I were to take a stab at a solution, we'd want something like the following. But we ideally want someone from the CakePHP community to chime in here. EDIT: Note that this seems to be for CakePHP 3.0, only.
// Build the query
$query = TableRegistry::get('controls')
->find()
->where([
'owner_id' => $ownerId,
'control_frequency_id' => $frequencyId,
'OR' => [
['date_last_copied IS' => null],
['date_last_copied <' => $date]
]
]);
// To make sure the query is what we wanted
debug($query);
// To get all the results of the query
foreach ($query as $control) {
. . .
}
I'm suggesting this, rather than the raw SQL string you have above, because:
We can now leverage the ORM model of CakePHP.
We don't have to worry about SQL injection, which you're currently vulnerable to.
EDIT: OK, this is a guess at the syntax applicable for CakePHP 2.0... YMMV
$controls = $this->controls->find('all', [
'conditions' => [
'owner_id' => $ownerId,
'control_frequency_id' => $frequencyId,
'OR' => [
['date_last_copied IS' => null],
['date_last_copied <' => $date]
]
]
];
Otherwise, we just use the raw query as a prepared statement:
$result = $this->getDataSource()->fetchAll("
SELECT *
FROM controls
WHERE owner_id = ?
AND control_frequency_id = ?
AND (
date_last_copied IS NULL
OR date_last_copied < ?
)",
[$ownerId, $frequencyId, $date]
);

Not sure about your whole logic but your final query statement should be something like:
SELECT * FROM `controls` WHERE (`owner_id` = <some owner_id>)
AND (`control_frequency_id` = <some id value>)
AND (`date_last_copied` = '' OR
`date_last_copied` IS NULL OR
`date_last_copied` < CURDATE() )
Use parentheses carefully to match your logic.

Always specify the version of cakePHP you are using for your App.
This query should work fine in CakePHP 3.0 for SQL AND and OR.
$query = ModelName>find()
->where(['colunm' => 'condition'])
->orWhere(['colunm' => 'otherCondition'])
->andWhere([
'colunm' => 'anotherContion',
'view_count >' => 10
])
->orWhere(['colunm' => 'moreConditions']);

Related

Not equal condition with findAll()

I am trying to pull record from a table using the following code
$userId = Yii::$app->user->id;
$lists = PromoLists::findAll(['user_id' => $userId, 'list_type' => 'custom']);
which outputs a query like below
select * from promo_lists where user_id ='$userId' and list_type='custom'
But i am unable to find any thing in the documentation that would help me achieve it with the following condition.
select * from promo_lists where user_id ='$userId' and list_type='custom' and status!='deleted'
as the status is an ENUM field and there are 4 different status
'active','pending','rejected','deleted'
currently i used the following approach
PromoLists::findAll(['user_id' => $userId, 'list_type' => 'custom', 'status'=>['active','pending','rejected']]);
which outputsthe following query
select * from promo_lists where user_id ='$userId' and list_type='custom' and status in ('active','pending','rejected')
which somehow achieves the same thing but this query would need to be edited every time when there is a new status type added to the table column status.
i know i can do this by using PromoLists::find()->where()->andWhere()->all()
but how to check with != / <> operator using findAll().
Simply like this:
PromoLists::find()->where(['and',
[
'user_id' => $userId,
'list_type' => 'custom',
],
['<>', 'status', 'deleted'],
])->all();
Using operator format in condition
http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#operator-format
PromoLists::find()
->andWhere([
'user_id' => $userId,
'list_type' => 'custom',
['!=', 'status', 'deleted']
])
->all();

Possible to mix where() clause

I have the following code (it's shortened otherwise it would be too long to show):
Feedback::find()
->where(['feedback.fg_id' => $this->id])
->orWhere(['feedback.fg_id' => $this->id, 'feedback.closed_time' => NULL, '<' => ['feedback.survey_end_date', new Expression('NOW()')]])
->all();
This code do not work, because of the last statement in orWhere()
My question is now: How can I combine the syntax inside orWhere? It's because I need all the statements inside the or block.
The ambivalent sql code should be:
SELECT * FROM feedback WHERE
(feedback.fg_id = 1) OR (feedback.fg_id = 1 AND feedback.closed_time IS NULL AND feedback.survey_end_date < NOW())
Yes you can combine you condition as ...
Feedback::find()
->where(['feedback.fg_id' => $this->id])
->orWhere(['AND',
['feedback.fg_id' => $this->id],
['feedback.closed_time' => NULL],
['<' , 'feedback.survey_end_date', new Expression('NOW()')]
]);

Cakephp find joining two columns

I have a BD that contains people with day and month fields, like so:
person1 (day = 5; month = 3)
person2 (day = 2; month = 12)
I have to perform a find between 2 dates, for example, I need all people between 01/03 and 01/06 (day/month) but I don't know how to perform that.
I tried using separate conditions, like this:
$conditions['People.day >='] = dayA;
$conditions['People.month >='] = monthA;
$conditions['People.day <='] = dayB;
$conditions['People.month <='] = monthB;
But, that's not correct because it finds day and month, I mean, it finds People between monthA and monthB and People between dayA and dayB, instead, what I need is people between dayA/monthA and dayB/monthB
I suppose I must do some kind of a JOIN, but I'm lost here, I looked some information but I don't know where to start.
updated info:
ok, I'm using this
Array
(
[People.month_day BETWEEN ? AND ?] => Array
(
[0] => 01/02
[1] => 28/02
)
)
but I get people like this:
1/1
2/1
10/1
11/1
12/1
13/1
20/1
21/1
1/2
what's wrong? do you need more code? I'm using this to retrieve results:
A paginate:
public $paginate = array('People'=>array(
'limit' => 16,
'order' => 'People.month, People.day ASC'
));
In your People model
public $virtualFields = array(
'month_day' => 'CONCAT(People.month, "-", People.day)'
// 'month_day' => 'CONCAT(People.day, "/", People.month)'
);
then add in your controller
$options = array(
'conditions' => array(
'Post.month_day BETWEEN ? AND ?' => array('01-03','01-06')
// 'Post.month_day BETWEEN ? AND ?' => array('03/01','06/01')
)
);
$posts = $this->Post->find('all',$options);
It would be best to alter your table. Use DATE field and your SQL query would be:
SELECT *
FROM dbname.People
WHERE People.date BETWEEN '1999-03-01' AND '1999-06-01';

Codeigniter multi select query

I have tried to make codeigniter query from mysql query
my mysql query is:
select *
from class_routine
where
semester='$semester'
and day='$day'
and time_schedule='$time_schedule'
and (batch='$batch' or section='$section' or teacher='$teacher' or room='$room');
for above query what will be the codeigniter query ?
I will use this query in model.
First, you should consider looking at the Documentation before asking any question, then specify what you've looked at and what you did try, posting your code.
Anyways, try this and let me know:
$this->db->where(array('semester' => $semester, 'day' => $day, 'time_schedule' => $time_scehdule));
$this->db->where("batch = $batch OR section = $section OR teacher = $teacher OR room = $room", NULL, FALSE);
$result = $this->db->get('class_routine')->result();
When in doubt is perfectly safe to use a normal query:
$result = $this->db->query("SELECT * FROM ....")->result();

Yii2 merge queries like cdbcriteria in yii1

I have a problem regarding merge of multiple queries.
In Yii 1.x you could merge a CDbCriteria with
$criteria->merge($otherCriteria)
How can I achieve the same nested conditions etc with queries in Yii2?
Edit:
Let's say I want separate queries to form subqueries. And after all subqueries are done I want to merge them together to the one big query.
There is no CDbCriteria concept in Yii2 anymore. Instead you can refer to the following classes:
http://www.yiiframework.com/doc-2.0/yii-db-query.html (yii\db\Query)
http://www.yiiframework.com/doc-2.0/yii-db-activequery.html (yii\db\ActiveQuery)
All you did before with CDbCriteria now you can do with above classes. So, there will be no need to merge two criteria with each other.
update
Yii2 also supports sub-queries like below (as Yii2's official guide):
$subQuery = (new Query)->select('COUNT(*)')->from('user');
$query = (new Query)->select(['id', 'count' => $subQuery])->from('post');
Which results in:
SELECT `id`, (SELECT COUNT(*) FROM `user`) AS `count` FROM `post`
http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#building-query
I also recently ran into this issue. Complete fusion of queries (select, join etc.) does not exist (as I understand it). But you can manually merge conditions, for example:
$query1 = \app\models\User::find()
->where(['column1' => 'value1', 'column2' => 'value2']);
$query2 = \app\models\User::find()
->where(['and', '[[column3]] = :column3', '[[column4]] = :column4'])
->addParams([
':column3' => 'value3',
':column4' => 'value4'
]);
// merge conditions
$query1->orWhere($query2->where);
$query1->addParams($query2->params);
// build SQL
echo $query1->createCommand()->rawSql;
Built SQL:
SELECT * FROM `yii2_user` WHERE
((`column1`='value1') AND (`column2`='value2')) OR
((`column3` = 'value3') AND (`column4` = 'value4'))
In addition to both the excellent answers above, should you need to merge two conditions to send on to a method which takes a 'condition' parameter in the same format as where(), then you can either build your own array:
$condition1 = ['column1' => 'value1', 'column2' => 'value2'];
$condition2 = ['column3' => 'value3', 'column4' => 'value4'];
$condition = [
'or',
$condition1,
$condition2,
];
$model->updateAll([ 'column5' => 'value5' ], $condition);
Or, if it feels more logical, you can use a temporary query object to build the conditions up and merge, etc. Then pass the generated where condition from it, e.g.:
$query1 = new \yii\db\Query;
$query1->andWhere(['column1' => 'value1', 'column2' => 'value2']);
$query2 = new \yii\db\Query;
$query2->andWhere(['column3' => 'value3', 'column4' => 'value4']);
$query = new \yii\db\Query;
$query->where($query1->where)
->orWhere($query2->where);
$model->updateAll([ 'column5' => 'value5' ], $query->where);
Obviously, this makes more sense when the queries or conditions are built up elsewhere in the code and separately passed the the place where the model method is executed.