This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Grouping WHERE clauses with Zend_Db_Table_Abstract
I need to create something like this:
select name from table where active = 1 AND (name LIKE 'bla' OR description LIKE 'bla')
The first part is easy:
$sqlcmd = $db->select()
->from("table", "name")
->where("active = ?", 1)
Now comes the tricky part. How can I nest? I know that I can just write
->orWhere("name LIKE ? OR description LIKE ?", "bla")
But thats wron, because I need to dynamically change all the parts. The query will be built all the time the script runs. Some parts get deleted, some altered. In this example I need to add those OR-s because sometimes I need to search wider.
"My Zend Logic" tells me that the correct way is like this:
$sqlcmd = $db->select()
->from("table", "name")
->where("active = ?", 1)
->where(array(
$db->select->where("name LIKE ?", "bla"),
$db->select->orWhere("description LIKE ?", "bla")
))
But that doesn't work (atleast I dont remember it working).
Please. Can someone help me to find a object oriented way for nesting "where"-s
Here's an example from the ZF manual
// Build this query:
// SELECT product_id, product_name, price
// FROM "products"
// WHERE (price < 100.00 OR price > 500.00)
// AND (product_name = 'Apple')
$minimumPrice = 100;
$maximumPrice = 500;
$prod = 'Apple';
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where("price < $minimumPrice OR price > $maximumPrice")
->where('product_name = ?', $prod);
It should fit your needs
To build this query:
SELECT product_id, product_name, price
FROM "products"
WHERE (price > 100.00)
AND (price < 500.00)
use this code:
$minimumPrice = 100;
$maximumPrice = 500;
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where('price > ?', $minimumPrice)
->where('price < ?', $maximumPrice);
Related
Is there any way to do something like this:
SELECT * FROM table WHERE p1=1 AND p2=2 AND ( p3 like %string1% OR p3 like %string3% )
In Zend Framework 1 by Zend_Db_Select or something else ?
Not sure about how you would go about doing such complex nested query using Zend_Db_Select but you can write can consider writing query manually as follows -
$sql = 'SELECT * FROM table WHERE p1 = ? AND p2 = ? AND (p3 LIKE ? OR p3 LIKE ?)';
$db->fetchAll($sql, [$p1, $p2, "%{$p3}%", "%{$p4}%"]);
You should rather use Zend_Db_Statement, which gives flexible options for fetching result sets. You can use:
$stmt = $db->query(
'SELECT * FROM table WHERE p1 = ? AND p2 = ? AND (p3 LIKE ? OR p3 LIKE ?',
array('1', '2', '%string1%', '%string3%')
);
I want to convert this query in yii
SELECT count(*) AS cnt, date(dt) FROM tbl_log where status=2 GROUP BY date(dt)
and fetch data from that. I try this command (dt is datetime field):
$criteria = new CDbCriteria();
$criteria->select = 'count(*) as cnt, date(dt)';
$criteria->group = 'date(dt)';
$criteria->condition = 'status= 2';
$visit_per_day = $this->findAll($criteria);
but no data will fetch!
wath can I do to get data?
Probably you see no data because you need assign data to model attributes which doesn't exist.
$criteria = new CDbCriteria();
$criteria->select = 'count(*) AS cnt, date(dt) AS dateVar';
$criteria->group = 'date(dt)';
$criteria->condition = 'status= 2';
$visit_per_day = $this->findAll($criteria);
This means that your model must have attributes cnt and dateVar in order to show your data. If you need custom query then check Hearaman's answer.
Try this below code
$logs = Yii::app()->db->createCommand()
->select('COUNT(*) as cnt')
->from('tbl_log') //Your Table name
->group('date')
->where('status=2') // Write your where condition here
->queryAll(); //Will get the all selected rows from table
Number of visitor are:
echo count($logs);
Apart from using cDbCriteria, to do the same check this link http://www.yiiframework.com/forum/index.php/topic/10662-count-on-a-findall-query/
If you use Yii2 and have a model based on table tbl_log, you can do it in model style like that:
$status = 2;
$result = Model::find()
->select('count(*) as cnt, date(dt)')
->groupBy('date(dt)')
->where('status = :status')
->params([':status' => $status ])
->all();
I am using Mertol Kasanan's class for running parametrized queries -
http://liveplanet.googlecode.com/svn-history/r132/trunk/db/DB.php
I am very satisfied with the script except for some issues that I don't seem to put my finger on.
As it states in the brief tutorial in the class's description the method for running the query is:
$result = $db->query('SELECT * FROM `users` WHERE id = ? AND user_type = ? LIMIT ?',$id,$user_type,$limit);
Can anybody figure out how to run a query without defining any parameter as it seems that
$result = $db->query('SELECT * FROM `users` WHERE id = 'y' ");
neither
$result = $db->query('SELECT * FROM `users` WHERE id = 'y' ", '');
do not do the trick as it returns a binding error;
A workaround would be
$result = $db->query('SELECT * FROM `users` WHERE 1 = ? AND id = 'y' ", 1);
Is there a neater way to run my query?
I don't need parameters as the query gets it's values from a safe source inside a class.
Edit:
Let's say I have this:
if($HC == 'C'){
$sql = "SELECT * FROM `photo_c` WHERE `user` = ?i AND `pic` != ?s AND cat != 'D' GROUP BY pic LIMIT ?";
$query = $this->dbs->query($sql,$this->user,$this->user_head,4);
$results = $this->dbs->numRows($query);
if($results < 3){
$sql = "SELECT * FROM `photo` WHERE `user` = ?i AND `pic` != ?s ORDER BY id ASC LIMIT ?";
$query = $this->dbs->query($sql, $this->user,$this->user_head,4);
}
}else{
$sql = "SELECT * FROM `photo_c` WHERE `user` = ?i AND `pic` != ?s AND cat = ?s ORDER BY RAND() LIMIT ?";
$query = $this->dbs->query($sql,$this->user,$this->user_head,$HC,4);
$results = $this->dbs->numRows($query);
}
Now, in order to get the data from the right query I can either define $data->getAll under each query - but that would mean repeating my code or I could try extracting the data from the last defined $query result - which I do not know how to do.
I know that there may be a better way of doing this but I am trying to improve my coding style as I think the safemysql class would need some improvements even if that would mean a bit more documentation.
I could try using $db->getAll instead of $db->query but, as far as I know, I cannot use numRows on GetAll.
As a matter of fact, this class is totally unusable. And the problem you mentioned is a least one.
It seems that someone who wrote it, never used this class in a real life project.
So, if you want a class which works and works way better, go for SafeMysql, as it will do exactly what you want:
$data = $db->getAll("SELECT * FROM `users` WHERE status = 'y'");
(note that you've got your data already, without any further code)
Nevertheless, you have to understand that the following statement of yours
I don't need parameters as the query gets it's values from a safe source inside a class.
is wrong.
It's OK to use a hard-coded value as you wrote it, but if you were intended to use a "safe" variable - it ought to be added via placeholder. Otherwise your query remains error-prone and unsafe.
So, it have to be
$id = 1; // "safe" variable
$data = $db->getRow("SELECT * FROM `users` WHERE id = ?i", $id);
To answer edited question. Not sure if it's what you need, but here is the code. It wu
if($HC == 'C')
{
$sql = "SELECT * FROM `photo_c` WHERE `user` = ?i AND `pic` != ?s AND cat != 'D' GROUP BY pic LIMIT ?";
$data = $this->dbs->getAll($sql,$this->user,$this->user_head,4);
if (count($data) < 3) {
$sql = "SELECT * FROM `photo` WHERE `user` = ?i AND `pic` != ?s ORDER BY id ASC LIMIT ?";
$data = $this->dbs->query($sql, $this->user,$this->user_head,4);
}
} else {
$sql = "SELECT * FROM `photo_c` WHERE `user` = ?i AND `pic` != ?s AND cat = ?s ORDER BY RAND() LIMIT ?";
$data = $this->dbs->query($sql,$this->user,$this->user_head,$HC,4);
}
I have a photos table where users can have multiple photos.
I'm trying to create the following query:
$q = Doctrine_Query::create()
->update('Photo p')
->set('p.photo_type', 1)
->where('p.user_id = ?', $id)
->andWhere('p.date_added = (SELECT MIN(p.date_added) FROM Photo p WHERE p.user_id = ?)', $id)
The idea is to set the "photo_type" to 1 for that specific photo of this user that has the minimum date added (earliest photo).
I just can't seem to get the syntax right. Can someone point me in the right direction?
Thank you.
EDIT:
It seems I'm trying to do something that can't be done, as per the answer to this question (MySQL Error 1093 - Can't specify target table for update in FROM clause). This question can be closed.
Try this:
$q = Doctrine_Query::create()
->update('Photo p')
->set('p.photo_type', 1)
->where('p.user_id = ?', $id)
->orderBy('p.date_added', 'desc')
->limit(1);
I searched the Web and could not find anything that would show me a good solid example. My question is basically this:
How do I convert this:
SELECT * FROM table WHERE ((a = 1 AND b = 2) OR (c = 3 OR c = 4)) AND d = 5;
To Zend syntax similar to this:
$this
->select()
->from($this->_schema.'.'.$this->_name)
->where('a = ?', '1');
So how can it be done?
Thank a lot in advance.
I had a similar problem. See the code example in the answer here: Grouping WHERE clauses with Zend_Db_Table_Abstract
So you would end up with something like:
$db = $this->getAdapter();
$this->select()
->where('(' . $db->quoteInto('a = ?', 1) . ' AND ' . $db->quoteInto('b = ?', 2) . ') OR (' . $db->quoteInto('c = ?', 3) . ' OR ' . $db->quoteInto('c = ?', 4) . ')')
->where('d = ?', 5);
Which would give you:
SELECT `table_name`.* FROM `table_name` WHERE ((a = 1 AND b = 2) OR (c = 3 OR c = 4)) AND (d = 5)
1) Build a condition for all groups Where/orWhere:
$conditions = $this->select()
->where('a= ?', 5)
->orWhere('b= ?', 6)
->getPart(Zend_Db_Select::WHERE);
// result: $conditions = "(a= 5) OR (b= 6)";
Use getPart() method to get the where condition.
2) Next, reset the where part of current select object:
$this->select()->reset(Zend_Db_Select::WHERE);
3) Finally, use where condition as you want:
$this->select()
->where('d= ?', 5)
->where(implode(' ', $conditions));
http://framework.zend.com/manual/1.12/ru/zend.db.select.html
Per a message board post on the Zend Framework website, this may not be possible.
It seems to me that where() and orWhere() in the Zend_Db_Select class are not enough to be able to write all queries. It does not support the nesting of conditions, which doesn't enforce the user with abstraction in somewhat more complex cases. With where() and orWhere() I cannot write this:
Edit
The array functionality of Zend_Db_Select->where is designed only for using it with the IN clause.
Example #17 Example of an array parameter in the where() method
// Build this query:
// SELECT product_id, product_name, price
// FROM "products"
// WHERE (product_id IN (1, 2, 3))
$productIds = array(1, 2, 3);
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where('product_id IN (?)', $productIds);
Original
As Peder said you can't nest orWhere but you can pass multiple arguments into where and orWhere.
$this->select()
->from($this->_schema.'.'.$this->_name)
->where(' ( a = ? AND b = ? ) OR ( c = ? OR c = ? ) ', array(1,2,3,4))
->where('d = ?',array(5));