Hi iam trying to covert this query as active record format
SELECT cmaindb, eshdr1, ascsnr, takpplz, takpadr
FROM ts_stats
WHERE (cmaindb = 'tam' OR cmaindb = 'soc')
AND (nproduktiv = 1)
OR (cmaindb = 'tc')
AND (nabrfirm = 5)
AND (nproduktiv = 1)
code which i have tried
return self::find()
->andWhere(['or',
['cmaindb'=> 'tam'],
['cmaindb'=> 'soc']
])
->andWhere(['nproduktiv'=>1])
->orWhere(['cmaindb' => 'tc'])
->andWhere(['nabrfirm '=>5, 'nproduktivs'=>1])
->all();
this will fails
Try this one
$cmaindbs = array('tam'=>'tam', 'soc'=>'soc');
return self::find()
->where(['IN', 'cmaindb' , $cmaindbs])
->andWhere(['nproduktiv' => 1])
->orWhere(['cmaindb' => 'tc'])
->andWhere(['nabrfirm' => 5])
->andWhere(['nproduktivs' => 1])
->all();
Let's further optimize your query:
return self::find()
->where(['IN', 'cmaindb' , ['tam', 'soc', 'tc'])
->andWhere(['nproduktiv' => 1])
->andWhere(['nabrfirm' => 5])
->andWhere(['nproduktivs' => 1])
->all();
Related
[UPDATED] Fixed
I have an SQL statement like this
SELECT
jea.subemployer_id,
jea.employer_id,
jea.job_id
FROM
job_employer_assoc AS jea
INNER JOIN job AS j ON jea.job_id = j.job_id
INNER JOIN subemployer AS s ON jea.subemployer_id = s.subemployer_id
WHERE
j.deleted = 0
AND j.klq = 0
AND j.STATUS = 0
AND s.FUNCTION IN ( 0, 1 )
GROUP BY
job_id
And this is my Query Builder:
public function getAllValidEmployerID()
{
$resultQuery = (new Query())
->select(['jea.subemployer_id', 'jea.employer_id', 'jea.job_id'])
->from(['job_employer_assoc AS jea'])
->innerJoin('job AS j', ['jea.job_id' => 'j.job_id'])
->innerJoin('subemployer AS s', ['jea.subemployer_id' => 's.subemployer_id'])
->where(['j.deleted' => 0])
->andWhere(['j.klq' => 0])
->andWhere(['j.status' => 0])
->andWhere(['s.function' => [0, 1]])
->groupBy('job_id')
->all();
return $resultQuery;
}
Why the SQL statement returns values while Query Builder returns an empty array?
I finally find out why, ['jea.job_id' => 'j.job_id'] should be 'jea.job_id = j.job_id'
How can i build query with subexpression, without using yii\db\Expression and raw sql. For example this:
SELECT * FROM user WHERE archived = 3 AND ((group = 2 AND status = 3) OR (group = 3 AND status = 2));
You can build such condition using array expressions:
$users = (new Query())
->from('user')
->where(['archived' => 3])
->andWhere([
'or',
[
'group' => 2,
'status' => 3,
],
[
'group' => 3,
'status' => 2,
],
])
->all();
I have a column in a table where all the details of the bill are getting stored. My brother used PHP web project to do that. Now I am trying to deserialize in VB.NET
Here is the PHP code (Controller)
public function add_to_list() {
$this->load->model('product_model', 'Product');
$pdts = $this->Product->get_data(true);
$available = $this->in_cart($this->input->post('id'));
$cart_qty = $available ? $available : 0;
if (!is_numeric($this->input->post('qty')) || !$this->input->post('id')) {
$res['success'] = false;
$res['msg'] = "Please enter the valid input";
} else {
$discount_percentage = $this->input->post('discount_percentage') ? $this->input->post('discount_percentage') : 0;
$discount_rate = $pdts->selling_price * ($discount_percentage / 100);
$final = $pdts->selling_price - $discount_rate;
$igst = $pdts->igst;
$igst_amt = calculate_price_by_precentage($this, $final, $pdts->igst);
$gross = $pdts->selling_price;
$total = $final + $igst_amt;
// $total = $gross + $igst_amt;
$available_qty = 5;
$data = array(
'id' => $pdts->id,
'qty' => $this->input->post('qty'),
'price' => $pdts->final_price,
'name' => $pdts->product,
'options' => array(
'product_id' => $pdts->id,
'product_hsn' => $pdts->hsn,
'product_name' => $pdts->product,
'product_unit' => $pdts->unit,
'product_rate' => $pdts->selling_price,
'product_mrp' => $pdts->mrp,
'product_gst' => $pdts->igst,
'product_cgst' => $pdts->cgst,
'product_gst_amount' => $igst_amt * $this->input->post('qty'),
'product_net_rate' => $pdts->cost_price,
'product_qty' => $this->input->post('qty'),
'product_free' => 0,
'product_discount_percentage' => $discount_percentage,
'product_discount_rate' => $discount_rate,
'product_total' => $total * $this->input->post('qty'),
'available_qty' => $available_qty,
'category' => $pdts->category,
'rate' => $pdts->selling_price,
'gross' => $gross,
'igst' => $igst,
'cgst' => $igst / 2,
'sgst' => $igst / 2,
'igst_amount' => $igst_amt,
'cgst_amount' => $igst_amt / 2,
'sgst_amount' => $igst_amt / 2,
'total' => $total //final_price($this, $pdts->selling_price, calculate_price_by_precentage($this, $pdts->selling_price, $pdts->discount), calculate_price_by_precentage($this, $pdts->selling_price, $pdts->sgst))
)
);
$this->cart->insert($data);
$this->pageViewData['final_discount'] = $this->input->post('discount') == '' ? '0' : $this->input->post('discount');
$this->pageViewData['final_discount_percentage'] = $this->input->post('discount_percentage') == '' ? '0' : $this->input->post('discount_percentage');
$this->pageViewData['ajax'] = true;
$res['success'] = true;
$res['msg'] = $this->load->view('order/cart_item', $this->pageViewData, true);
}
echo json_encode($res);
}
And this is how the cell Value looks.
> a:1:{s:32:"b3712e169500f4754be4a6a681220a96";a:7:{s:2:"id";s:2:"69";s:3:"qty";d:1;s:5:"price";d:26.25;s:4:"name";s:28:"Seeded
> Dates 200gm Wet
> Dates";s:7:"options";a:26:{s:10:"product_id";s:2:"69";s:11:"product_hsn";s:8:"08041020";s:12:"product_name";s:28:"Seeded
> Dates 200gm Wet
> Dates";s:12:"product_unit";s:2:"GM";s:12:"product_rate";s:5:"26.25";s:11:"product_mrp";s:2:"35";s:11:"product_gst";s:2:"12";s:12:"product_cgst";s:1:"6";s:18:"product_gst_amount";d:3.149999999999999911182158029987476766109466552734375;s:16:"product_net_rate";s:5:"24.70";s:11:"product_qty";s:1:"1";s:12:"product_free";i:0;s:27:"product_discount_percentage";i:0;s:21:"product_discount_rate";d:0;s:13:"product_total";d:29.39999999999999857891452847979962825775146484375;s:13:"available_qty";i:5;s:8:"category";s:9:"AD
> Seeded";s:4:"rate";s:5:"26.25";s:5:"gross";s:5:"26.25";s:4:"igst";s:2:"12";s:4:"cgst";i:6;s:4:"sgst";i:6;s:11:"igst_amount";d:3.149999999999999911182158029987476766109466552734375;s:11:"cgst_amount";d:1.5749999999999999555910790149937383830547332763671875;s:11:"sgst_amount";d:1.5749999999999999555910790149937383830547332763671875;s:5:"total";d:29.39999999999999857891452847979962825775146484375;}s:5:"rowid";s:32:"b3712e169500f4754be4a6a681220a96";s:8:"subtotal";d:26.25;}}
That looks a lot to me. As a beginner, I need a big help from you guys to get this done.
I am trying to build a query using query builder with complex nested AND and OR conditions. Here is what I have written so far.
$cond_arr = array();
$cond_arr['VehicleBrandModels.status'] = 1;
$query = $this->VehicleBrandModels->find();
$query->hydrate(false);
$query->select($this->VehicleBrandModels);
$query->where($cond_arr);
$VBM_data = $query->toArray();
This will generate a query like below
SELECT * FROM vehicle_brand_models WHERE status = 1;
I want to generate a query with nested AND & OR conditions like below
SELECT * FROM vehicle_brand_models WHERE status = 1 AND ((overall_rating > 0 AND overall_rating < 2) OR (overall_rating >= 2 AND overall_rating < 4) OR (overall_rating >= 4 AND overall_rating <= 5));
Can anybody help to solve how to achieve this in CAKEPHP 3.0 Query builder?
The simplest solution is the following
$cond_arr = [
'VehicleBrandModels.status' => 1,
'OR' => [
['overall_rating >' => 0, 'overall_rating <' => 2],
['overall_rating >=' => 2, 'overall_rating <' => 4],
['overall_rating >=' => 4, 'overall_rating <=' => 5]
]
];
there is a more 'cake' way and if I'll have time I will post it.
But note that from what I see all your OR conditions overlap and you can simply do
$cond_arr = [
'VehicleBrandModels.status' => 1,
'overall_rating >' => 0,
'overall_rating <=' => 5
];
edit: as primised here's the more cake way using query expressions
$query->where(function($exp, $q) {
$exp = $exp->eq('VehicleBrandModels.status', 1);
$conditions1 = $exp->and_([])
->gt('overall_rating ', 0)
->lte('overall_rating ', 2);
$conditions2 = $exp->and_([])
->gt('overall_rating ', 2)
->lte('overall_rating ', 4);
$conditions3 = $exp->and_([])
->gt('overall_rating ', 4)
->lte('overall_rating ', 5);
$orConditions = $exp->or_([$conditions1, $conditions2, $conditions3]);
$exp->add($orConditions);
return $exp;
});
still some conditions are overlapping
Here is my code. I want to select results from two columns into one. Thanks.
$messages = (new \yii\db\Query())
->select(['idsender as idotherguy'])
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id])
->all();
$messages2 = (new \yii\db\Query())
->select(['idreceiver as idotherguy'])
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id])
->all();
$messages->union($messages2);
after advice here I also tried....
$messages = (new \yii\db\Query())
->select(['idsender as idotherguy'])
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id]);
$messages2 = (new \yii\db\Query())
->select(['idreceiver '])
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id]);
$messages->union($messages2);
$messages->all();
and i get query object
yii\db\Query Object
(
[select] => Array
(
[0] => idsender as idotherguy
)
[selectOption] =>
[distinct] =>
[from] => Array
(
[0] => messages
)
[groupBy] =>
[join] =>
[having] =>
[union] => Array
(
[0] => Array
(
[query] => yii\db\Query Object
(
[select] => Array
(
[0] => idreceiver as idotherguy
)
[selectOption] =>
[distinct] =>
[from] => Array
(
[0] => messages
)
[groupBy] =>
[join] =>
[having] =>
[union] =>
[params] => Array
(
)
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
[where] => Array
(
[idsender] => 2
)
[limit] =>
[offset] =>
[orderBy] =>
[indexBy] =>
[emulateExecution] =>
)
[all] =>
)
)
[params] => Array
(
)
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
[where] => Array
(
[idreceiver] => 2
)
[limit] =>
[offset] =>
[orderBy] =>
[indexBy] =>
[emulateExecution] =>
)
that is it... I tried also answe bellow and used in select 'idreceiver as idotherguy' ... but the result is the same
you should avoid the alias in united query , use literal select format and apply all to the result only
$messages = (new \yii\db\Query())
->select('idsender as idotherguy')
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id]);
$messages2 = (new \yii\db\Query())
->select('idreceiver')
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id]);
$messages->union($messages2);
$messages->all();
for see the result you should
foreach($messages as $key=> $value) {
echo $value->idotherguy;
}
or if the result is an array
foreach($messages as $key=> $value) {
echo $value['idotherguy'];
}
try check the ral sql code this way (instead of $messages->all();)
var_dump( $messages->createCommand()->sql);
Take a look at the documentation http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#union, you shouldn't have all() in your queries
$messages = (new \yii\db\Query())
->select(['idsender as idotherguy'])
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id]);
$messages2 = (new \yii\db\Query())
->select(['idreceiver as idotherguy'])
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id]);
$messages->union($messages2);
$messages->all();
I changed the code offered here like this and it works now fine....
$messages = (new \yii\db\Query())
->select(['idsender as idotherguy'])
->from('messages')
->where(['idreceiver' => Yii::$app->user->identity->id]);
$messages2 = (new \yii\db\Query())
->select(['idreceiver as idotherguy'])
->from('messages')
->where(['idsender' => Yii::$app->user->identity->id]);
$messages = $messages->union($messages2)
->all();
The result now is array with values as I wanted.
Array
(
[0] => Array
(
[idotherguy] => 3
)
[1] => Array
(
[idotherguy] => 11
)
[2] => Array
(
[idotherguy] => 10
)
)
Thanks for help boys :)