Can someone guide me to write a query like below using Zend db select :
SELECT `tbl_md_users`.*
FROM `tbl_md_users`
WHERE
user_type <> 'TYPE1')
AND (first_name LIKE '%tom%' OR last_name LIKE '%tom%' OR user_name LIKE '%tom%')
If you wanted
SELECT `tbl_md_users`.*
FROM `tbl_md_users`
WHERE
user_type <> 'TYPE1')
AND (first_name LIKE '%tom%' OR first_name LIKE '%dick%' OR first_name LIKE '%harry%')
then the first answer doesn't work
I used Zend_Db_Expr instead
$likeTom = new Zend_Db_Expr("first_name LIKE '%tom%'");
$likeDick = new Zend_Db_Expr("first_name LIKE '%dick%'");
$likeHarry = new Zend_Db_Expr("first_name LIKE '%harry%'");
$query = $database->select ()
->from ('tbl_md_users')
->where ('user_type <> ?', 'TYPE1')
->where ("{$likeTom} OR {$likeDick} OR {$likeHarry}");
$query = $database->select ()
->from ('tbl_md_users')
->where ('user_type <> ?', 'TYPE1')
->where ("first_name LIKE '%?%' OR last_name LIKE '%?%' OR user_name LIKE '%?%'", 'tom');
The current, up-to-date solution is to call nest() and unnest() in the where clause:
$select->where
->nest()
->equalTo('column1', 1)
->or
->equalTo('column2', 2)
->unnest()
->and
->equalTo('column3', 3);
I think there is no way to use Zend_Db_Select for this. You could use
$this->table->getAdapter()->quoteInto()
To write an custom query.
Related
code downstairs will perfom query like this
SELECT anrede FROM `l_anrede` INNER JOIN `person` ON `l_anrede`.id = `person`.id_anrede
But I need a query like this:
SELECT anrede FROM `l_anrede` INNER JOIN `person` ON `l_anrede`.id=`person`.id_anrede WHERE
person.id IN(SELECT id_person FROM bewerber WHERE person.id=bewerber.id);
Any ideas how to round out this code:
$query = LAnrede::find();
/*
$subQuery = Bewerber::find()->select(['id_person'])
->from('bewerber')->where('person.id= bewerber.id');*/
$query->select('anrede')->from('l_anrede')->innerJoin('Person', 'l_anrede.id = person.id_anrede');
var_dump($query->one());
You don't have to use ->from('l_anrede') if youre calling LAnrede::find() - query will select data from table associated with your model. To use subquery in where() method, try like this:
$query->andWhere(['person.id' => $subQuery]);
I would like to do a subselect in order to do the following postgresql query with the querybuilder:
SELECT i.* FROM internship i
WHERE EXISTS (SELECT iw.*
FROM internship_weeks iw
WHERE i.id = iw.internship)
Does anyone have an idea how to get the same result with queryBuilder? or maybe with DQL?
Thanks for the help !
As example, only for demonstrate HOW-TO use a subquery select statement inside a select statement, suppose we what to find all user that not yet have compile the address (no records exists in the address table):
// get an ExpressionBuilder instance, so that you
$expr = $this->_em->getExpressionBuilder();
// create a subquery
$sub = $this->_em->createQueryBuilder()
->select('iw')
->from(IntershipWeek::class, 'iw')
->where('i.id = iw.intership');
$qb = $this->_em->createQueryBuilder()
->select('i')
->from(Intership::class, 'u')
->where($expr->exists($sub->getDQL()));
return $qb->getQuery()->getResult();
Hope this help
I'm trying to write a query using a Zend_Db_Table object with multiple bound parameters. My query, before I put in the real values, was being run as follows:
$mytbl = new MyTable();
$myresults = $mytbl->fetchAll(
"(col1 LIKE 'val1' AND col2 LIKE 'val2')
OR (col1 LIKE 'val2' AND col2 LIKE 'val1'))
AND col3 = 'val3');
This works - except my variables are not being quoted properly. I tried to then replace the second line with:
$mytbl->fetchAll(
"(col1 LIKE ? AND col2 LIKE ?)
OR (col1 LIKE ? AND col2 LIKE ?))
AND col3 = ?,
array(
$db->quote($val1),
$db->quote($val2),
$db->quote($val2),
$db->quote($val1),
$db->quote($val3)));
but then I get an SQL error that the number of parameters does not match. So I tried:
$mytbl->fetchAll(
$mytbl->select()
->where("(col1 LIKE ? AND col2 LIKE ?)
OR (col1 LIKE ? AND col2 LIKE ?)) AND col3 = ?,
array(
$db->quote($val1),
$db->quote($val2),
$db->quote($val2),
$db->quote($val1),
$db->quote($val3))));
but I get the same error. If I put a single parameter, without the array, then it does the replacement properly, but with the array, it does not work. How can I bind multiple parameters like this?
just use IN and put the questionmark in brackets... example:
$productIds = array(1, 2, 3);
$select = $db->select()
->from('products',
array('product_id', 'product_name', 'price'))
->where('product_id IN (?)', $productIds);
You don't need to quote your variables, ZF takes care of this for you:
$mytbl->fetchAll(
"(col1 LIKE ? AND col2 LIKE ?)
OR (col1 LIKE ? AND col2 LIKE ?))
AND col3 = ?",
array($val1, $val2, $val3, $val4, $val5));
I try to build a query with subquery in FROM part using the Zend_Db_Select. Im looking for somthing like this:
SELECT COUNT(row_1) AS count_row FROM (SELECT row,row2,... FROM table WHERE row= ...) AS temp WHERE row = 0)
So I try to do it like this:
$oSubSelect =
$this->select()
->setIntegrityCheck(false)
->from('table',
array(
'row_id'
)
)
->where(PRFX.'table.id = '.PRFX.'table2.id')
->from(PRFX.'table2',array('row','row2'));
$this->select(false)
->setIntegrityCheck(false)
->from(new Zend_Db_Expr($oSubSelect).' AS temp',
array(
'COUNT(row_id) AS row_count',
)
);
But this gives me an error message.
Best regards.
Ok I fix this. The problem was in
->from(new Zend_Db_Expr($oSubSelect).' AS temp',
Should be:
->from(new Zend_Db_Expr('('.$oSubSelect.')'),
I want to use a query which uses LIKE .. for e.g select * from xxxx where zzzz LIKE 'a%';
How can I do that using Zend DB?
I have already tried something like $db->query('SELECT * FROM XXXX where zzzzz LIKE ?','\'' . $query .'%\''); but it is not working.
Thanks
You're double quoting. You don't need the escaped quotes around $query. Prepared statements will take care of that for you:
$db->query('SELECT * FROM XXXX where zzzzz LIKE ?', '%' . $query .'%');
$user = new Application_Model_DbTable_User();
$uname=$_POST['uname'];
$query = $user->select()->where('firstname LIKE ?', $uname.'%')->ORwhere('lastname LIKE ?', $_POST['lname'].'%')->ORwhere('emailid LIKE ?', $_POST['email'].'%');
$userlist = $user->fetchAll($query);