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);
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
));
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();
I have two tables, the report_details and the name. In the report_details table, I have for and from which is an id that is related to the table name. What is the proper syntax in Yii2 to get both the for and from the column on the name table? This is my query so far...
$query = new yii\db\Query;
$query->select('report_details.reference_no, report_details.subject, report_details.doc_for, report_details.doc_from, report_details.doc_date, report_details.doc_name, report_details.drawer_id, report_details.user_id, name.name_id, name.position, name.fname, name.mname, name.lname')
->from('report_details')
->join('LEFT JOIN', 'name', 'report_details.doc_for = name.name_id')
->where(['report_details.reference_no' => $model->reference_no]);
$results = $query->all();
you could use ->leftJoin( )
$query = new yii\db\Query;
$query->select('report_details.reference_no, report_details.subject,
report_details.doc_for, report_details.doc_from,
report_details.doc_date, report_details.doc_name,
report_details.drawer_id, report_details.user_id,
name.name_id, name.position, name.fname, name.mname, name.lname')
->from('report_details')
->leftJoin( 'name', 'report_details.doc_for = name.name_id')
->where(['report_details.reference_no' => $model->reference_no]);
$results = $query->all();
how can I select multiple or in codeigniter?
for exmaple, select * from bla where id = 1 or id = 2 or id = 3?
If I use this:
$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status);
it is with AND...
You can use the where_in method as a shortcut to multiple or-statements for the same column:
$available_ids = [1, 2, 3];
$this->db->where_in('id', $available_ids);
// WHERE id IN (1, 2, 3)
If you were looking to check multiple columns (the name is 'Adam' or the title is 'Grand Poobah' or the status is 'Active'), you can use the or_where method instead:
$this->db->where('name', $name);
$this->db->or_where('title', $title);
$this->db->or_where('status', $status);
// WHERE name = 'Adam' OR title = 'Grand Poobah' OR status = 'Active'
To put it all together, you'd
$available_ids = [1, 2, 3];
$query = $this->db->select('*')->from('bla')->where_in('id', $available_ids)->get();
// SELECT * FROM bla WHERE id IN (1, 2, 3)
CodeIgniter v3 Reference
CodeIgniter v2 Reference
Just try this
$array = array(
'name' => $name,
'title' => $title,
'status' => $status
);
$this->db->where($array);
Add all data to an array. Then pass the array to where clause.
Because my join includes a field named 'id' as well, I need to rename this field name during my sql so it won't override my id field name from the first selected tabel.
My query look likes as follow;
$select = new \Zend\Db\Sql\Select();
$select->from('websites');
$select->join(array('s' => 'websites_statistics'), 's.website_id = websites.id');
$select->where(array('websites.website' => $website));
$select->order('s.timestamp DESC')->limit(1);
$rowset = $this->tableGateway->selectWith($select);
$row = $rowset->current();
return $row;
So, 's' 'id' field should be renamed to something like 'stat_id'.
Thanks in advance!
Nick
$select = new \Zend\Db\Sql\Select();
$select->from('websites');
->join(array('s' => 'websites_statistics'), 's.website_id = websites.id',
array('stat_id' => 's.id')); // <-- here is the alias
->where(array('websites.website' => $website));
->order('s.timestamp DESC')
->limit(1);
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from(array('p' => 'sub_categories'), array('subcategory_id'=>'p.subcategory_id','subname'=>'p.name'))
->join(array('pa' => 'categories'), 'pa.category_id = p.category_id', array('catname'=>'pa.name'));
$result = $this->getAdapter()->fetchAll($select);
*
And also we can use this method
use Zend\Db\Sql\Expression;
->join(array('s' => 'websites_statistics'), 's.website_id = websites.id',
array('stat_id' => new Expression('s.id'))); // <-- here is the alias
This is best method If you have to use Mysql 'AS' on zf2
example : array('Month' => new Expression('DATE_FORMAT(`salesInvoiceIssuedDate`, "%m")'))