I want when currency = 'sum' sort by and Where(['between', 'price', $min_price, $max_price]) and when currency = 'y.e.' sort by andWhere(['between', 'price', $min_price*2, $max_price*2]). How to write the sql query in yii2?
$anmt_t = (new \yii\db\Query())
->select(['*'])
->from('anmt')
->where(['status' => 'confirmed'])
->andWhere(['between', 'price', $min_price, $max_price, when (currency = 'sum')])
->andWhere(['between', 'price', $min_price*2, $max_price*2, when (currency = 'y.e.')])
->all();
Try using CASE :
$anmt_t = (new \yii\db\Query())
->select(['*'])
->from('anmt')
->where(['status' => 'confirmed'])
->andWhere('
CASE
WHEN currency = "sum" THEN price BETWEEN :mp1 AND :mx1
WHEN currency = "y.e." THEN price BETWEEN :mp2 AND :mx2
END
')
->params([
'mp1' => $min_price,
'mx1' => $max_price,
'mp2' => $min_price * 2,
'mx2' => $max_price * 2,
])
->all();
Not tested
I'd personally favor using Yii2, rather than writing a long query
$condition = currency == 'y.e.' ? ['between', 'price', $min_price *2, $max_price*2] : ['between', 'price', $min_price, $max_price];
then
$anmt_t = (new \yii\db\Query())
->select(['*'])
->from('anmt')
->where(['status' => 'confirmed'])
->andWhere($condition)
->all();
Related
These are the values I want to insert into the database:
$name4 = $request['name4'];
$email4 = $request['email4'];
$phone = $request['phone'];
$cat = $request['cat'];
$rname = $request['rname'];
$file = $request['file'];
$ingr = $request['ingr'];
$qty = $request['qty'];
$unit = $request['unit'];
$recp = $request['recp'];
$items = array(
'ingredients' => $ingr,
'quantity' => $qty,
'unit' => $unit
);
And insert query is as follows:
DB::INSERT("insert into tbl_contest (name, email, phone, category, recp_name, file, ingredient, recp_desc)" . "values('$name4','$email4','$phone','$cat','$rname','$file','$items','$recp')");
I wanted to add $ingr, $qty and $unit into the column ingredient.
Can anyone help me?
Thanks in advance
The $items variable can not be an array, you can turn it into a json string.
$items= json_encode(array(
'ingredients' => $ingr,
'quantity' => $qty,
'unit' => $unit
));
Why this query is not working?
public function actionInsert()
{
$model = new NotificationsEvents();
$date_at = (new Query())
->select(['single-events.date_at'])
->from('single-events')
->leftJoin('user', 'user.birthday = single-events.date_at');
$event_id = (new Query())
->select(['single-events.id'])
->from('single-events')
->leftJoin('user', 'user.id = single-events.id');
(new Query())->createCommand()->insert('notifications_events', [
'type' => 7,
'date_at' => $date_at,
'event_id' => $event_id,
])->execute();
}
I need to insert user birthday in notifications_events.date_at, and user ID in notifications_events.event_id, but this code is not working:
Unknown column 'single' in 'on clause'
single-events is not a valid/safe name for table, because it contains -. You should either quote it in every possible place:
public function actionInsert() {
$model = new NotificationsEvents();
$date_at = (new Query())
->select(['{{single-events}}.date_at'])
->from('single-events')
->leftJoin('user', 'user.birthday = {{single-events}}.date_at');
$event_id = (new Query())
->select(['{{single-events}}.id'])
->from('single-events')
->leftJoin('user', 'user.id = {{single-events}}.id');
(new Query())->createCommand()->insert('notifications_events', [
'type' => 7,
'date_at' => $date_at,
'event_id' => $event_id,
])->execute();
}
Or use some more practical name for table, like single_events.
Another quoting method: using backtics:
`single-events`
When I want select where IN query, how to define multiple values in where clause?
Note on the ':k'=>1 and ':k'=>, how to use it for 2 values?
$query = Model::find()->where('id = :id and type = :k' ,[':id'=>$id, ':k'=>1,':k'=>27])->count();
Conditions can be also defined using array syntax:
$count = Model::find()
->where([
'AND',
['=', 'id', $id],
['IN', 'type', [1, 27]],
])
->count();
You could try using IN baded on an array
assuming
$myArray = array(1,27);
$query = Model::find()->where(['IN', 'id', $myArray])
->andWhere('id = :id', [':id' => $id])->count();
You can do it in this way:
$types = [1, 27];
$query = Model::find()
->where(['id' => $id])
->andWhere(['type' => $types])
->count();
Yii2 will convert your $types array to IN condition. SQL query will be:
SELECT COUNT(*) FROM <table> WHERE id = <id> AND type IN (1, 27);
I want to convert following complex mysql into cake's find expression:
SELECT p1, p2
FROM
(
SELECT IFNULL(a.c2, '10') AS p1, IFNULL((SELECT MAX(c.c1) FROM my_table c WHERE c.c1>p1), '30') AS p2
FROM my_table a
WHERE
(
(a.user_id = 2) AND (a.c1 BETWEEN '10' AND '30')
)
) as temp
WHERE p2 > 100
ORDER BY p1;
I tried following
http://dogmatic69.com/sql-to-cakephp-find-converter
but unable to generate the desired expression.
Please help. I really don't know how to handle such complex expressions (I do not prefer to use query in cakephp)
Thanks
Let me just convert your query into the cakephp way
if table a's model is A:
$fields = "IFNULL(A.c2, '10') AS p1, IFNULL((SELECT MAX(C.c1) FROM my_table c WHERE C.c1>p1), '30') AS p2";
$conditions = "A.user_id=2 AND A.c1 BETWEEN '10' AND '30'";
$inner_querry = $this->A->find("all", compact("fields", "conditions"));
$fields = "p1,p2";
$conditions = "p1 IN ($inner_querry) AND p2 IN($inner_query) AND p2 > 100";
$order = "p1";
$query = $this->A->find("all", compact("fields", "conditions", "order"));
debug($query); //check results of for error.
I think that sometimes it's not so wrong to use Model->query(), but let' try to use cake functions.
In fact the only way I see to obtain that particular query is building the subqueries with buildStatement() function (and at the end you still have to call Model->query() so...). But just for fun.
Assuming your model name is MyTable, in your MyTablesController do:
$p1 = "IFNULL(a.c2, '10') AS p1";
$db = $this->MyTable->getDataSource();
$subQuery = $db->buildStatement(
array(
'table' => $db->fullTableName($this->MyTable),
'alias' => 'C',
'fields' => array('MAX(c.c1)'),
'conditions' => array(
"C.c1 > " => 'p1',
)
),
$this->MyTable
);
$p2 = "IFNULL(".$subQuery.") AS p2";
$subQuery = $db->buildStatement(
array(
'table' => $db->fullTableName($this->MyTable),
'alias' => 'A',
'fields' => array($p1, $p2),
'conditions' => array(
"A.user_id" => 2,
"A.c1 BETWEEN ? AND ?" => array(10,30)
)
),
$this->MyTable
);
$query = $db->buildStatement(
array(
'table' => $subQuery,
'alias' => 'test',
'fields' => array('p1', 'p2'),
'conditions' => array(
"p2 > " => 100,
)
),
$this->MyTable
);
Basically i need to have this query done through zend framework.
SELECT k.id AS ID ,k.name AS NAME ,k.ppu_sell AS PRICE, k.type as TYPE FROM `inventory` as k UNION
select m.id AS ID, m.name AS NAME, m.price AS PRICE, 'menu' as TYPE FROM menu as m
Try this:
$select = Zend_Db_Table::getDefaultAdapter()->select();
$select->from(
array('inventory' => 'k'),
array(
'ID' => 'k.id',
'NAME' => 'k.name',
'PRICE' => 'k.ppu_sell',
'TYPE' => 'k.type'));
$selectClone = clone $select;
$select->reset()->from(
array('menu' => 'm'),
array(
'ID' => 'm.id',
'NAME' => 'm.name',
'PRICE' => 'm.price',
'TYPE' => new Zend_Db_Expr("'menu'")));
$select = Zend_Db_Table::getDefaultAdapter()->select()->union(array(
$selectClone, $select
));
Zend Framework Select Objects And UNION()