How would i convert below query to sub query?
I don't want to use JOINS I want to do same through sub query. i-e I need three subqueries out of the three joins. How it would be possible for below query ?
protected $_name = 'sale_package_features';
public function getAllSalePackageFeatures(){
$sql = $this->select()->setIntegrityCheck(false)
->from(array('spf' => $this->_name))
->joinLeft(array('sd' => 'sale_devices'),'sd.sale_device_id = spf.sale_device_id',array('sd.sale_device_name AS deviceName'))
->joinLeft(array('sp' => 'sale_packages'),'sp.sale_package_id = spf.sale_package_id',array('sp.sale_package_name AS packageName'))
->joinLeft(array('sf' => 'sale_features'),'sf.sale_feature_id = spf.sale_feature_id',array('sf.sale_feature_name AS featureName'))
->where('sf.parent_id != ?',0)
->order('spf.sale_package_feature_id ASC');
return $sql->query()->fetchAll();
}
Edited :
SELECT
`spf`.*, `sd`.`sale_device_name` AS `deviceName`,
`sp`.`sale_package_name` AS `packageName`,
`sf`.`sale_feature_name` AS `featureName`
FROM `sale_package_features` AS `spf`
LEFT JOIN `sale_devices` AS `sd`
ON sd.sale_device_id = spf.sale_device_id
LEFT JOIN `sale_packages` AS `sp`
ON sp.sale_package_id = spf.sale_package_id
LEFT JOIN `sale_features` AS `sf`
ON sf.sale_feature_id = spf.sale_feature_id
WHERE (sf.parent_id != 0)
ORDER BY `spf`.`sale_package_feature_id` ASC
You can use
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$subSelect = $dbAdapter
->select()
->from( 'tablename',
array(
'col1_alias' => 'column1_name',
'col2_alias' => 'column2_name',
))
->where('somefield = ?', $value_to_be_quoted);
$select = $dbAdapter
->select()
->from(array('sub_alias' => $subSelect ))
->where('otherfield = ?', $other_value_to_be_quoted);
$sql = $select->assemble();
$sql will contain
SELECT
sub_alias . *
FROM
(SELECT
tablename.column1_name AS col1_alias,
tablename.column2_name AS col2_alias
FROM
tablename
WHERE
(somefield = 'quoted_value')) AS sub_alias
WHERE
(otherfield = 'quoted_other_value')
If you need to use SELECT .. WHERE x IN (subselect) than go with
$select->where( 'column IN (?)', new Zend_Db_Expr($subselect->assemble()) );
Related
I am using Zend Framework 2.4 with TableGateway and need to execute the following query:
SELECT products.title, student.firstname, orders.order_id,
orders.profile_id, orders.status
FROM student
INNER JOIN
(products
INNER JOIN orders ON products.product_id = orders.product_id)
ON student.student_id = orders.student_id
WHERE (((orders.profile_id)='.$id.'));
Here is what I am using:
$select = $this->tableGateway->getSql()->select();
$select->columns(array('*'))
->join('products', 'products.product_id = orders.product_id', array('title'))
->join('student', 'student.student_id = orders.student_id', array('first_name'))
->where(array('orders.profile_id' => $id));
$items = $this->tableGateway->selectWith($select);
return $items;
The problem is that $items does not contain the columns title or first_name. It only contains the columns from the orders table.
What do I need to do to get $items to contain the columns from the joined tables?
try to use an associative array
$select = $this->tableGateway->getSql()->select();
$select->columns(array('*'))
->join('products', 'products.product_id = orders.product_id', array('title' => 'product_title'))
->join('student', 'student.student_id = orders.student_id', array('first_name' => 'student_first_name'))
->where(array('orders.profile_id' => $id));
$items = $this->tableGateway->selectWith($select);
return $items;
Maybe something simple but my query builder doesn't return what I want. I would something like ... where ... AND (.... OR ....)
I try to read the doctrine doc but, I'm not fluent english so I don't really understand the doctrine doc (maybe a translation in french is available ?).
<?php
// $qb instanceof QueryBuilder
$qb->select(array('u')) // string 'u' is converted to array internally
->from('User', 'u')
->where($qb->expr()->orX(
$qb->expr()->eq('u.id', '?1'),
$qb->expr()->like('u.nickname', '?2')
))
->orderBy('u.surname', 'ASC'));
Here is my MySQL test code that work
select count(distinct(u.username)) from fos_user u join users_accounts ua ON u.id=ua.user_id join account acc ON acc.id = ua.account_id where u.id=48 and (acc.id=2 OR acc.id=5)
Here is the query builder :
I use a service because I had to use this functionality more than once.
/**
* Check if User exists in one of the connected user (Admin or SupAdmin) accounts
* argument : userID
*
*/
public function userExists($userID)
{
// Return accounts (array)
$accounts = $this->accountManager->listAccountsByConnectedUser();
$repository = $this->em->getRepository('CMiNewsBundle:User');
$qb = $repository->createQueryBuilder('u');
$query = $qb
->select('count(u)')
->join ('u.accounts', 'acc')
->where('u.id = :userID')
->andwhere('acc.id = :accountID')
->setParameters(array(
'userID' => $userID,
'accountID' => $accounts[0]->getId(),
));
if (count($accounts) > 1) {
$accountMax = count($accounts);
for($acc=1; $acc<$accountMax; $acc++)
{
$query->orWhere('acc.id = :accountID_'.$acc.'')->setParameter('accountID_'.$acc.'', $accounts[$acc]->getId());
}
};
$query = $query->getQuery();
$result = $query->getResult();
return $result;
}
Thank you for your advices
You can rewrite your query as using IN() in mysql like below
SELECT
COUNT(DISTINCT (u.username))
FROM
fos_user u
JOIN users_accounts ua
ON u.id = ua.user_id
JOIN account acc
ON acc.id = ua.account_id
WHERE u.id = 48
AND acc.id IN(2,5) /* this is equivalent to acc.id = 2 or acc.id = 5 */
The current query for doctrine you are running is ignoring your and condition like if you have 2 accounts ids query will be
WHERE u.id = 48 AND acc.id = 2 OR acc.id = 5
So it will give records for acc.id = 5 and u.id can be any other id
For doctrine query you can rewrite your code as below,You have to build an array of account ids and pass this array in your IN() clause
public function userExists($userID)
{
// Return accounts (array)
$accounts = $this->accountManager->listAccountsByConnectedUser();
$repository = $this->em->getRepository('CMiNewsBundle:User');
$qb = $repository->createQueryBuilder('u');
$accounts=array();
$accounts[]=$accounts[0]->getId();
if (count($accounts) > 1) {
$accountMax = count($accounts);
for ($acc = 1; $acc < $accountMax; $acc++) {
$accounts[]=$accounts[$acc]->getId();
}
}
$query = $qb
->select('count(u) as your_count')
->join('u.accounts', 'acc')
->where('u.id = :userID')
->andwhere('acc.id IN(:accountID)')
->setParameters(array(
'userID' => $userID,
'accountID' => $accounts,
));
$query = $query->getQuery();
$result = $query->getResult();
return $result;
}
I am using PHPActiveRecord and CodeIgniter. I am trying to create a blog CMS and a blog has many categories and its referring to another table called blogrefs. So basically
blogs => blogrefs => blogcats
blogrefs is referring with blogs through blogrefs.blog_id
blogrefs is referring with blogcats through blogrefs.blogcat_id
function get_blogs($params = array()){
$CI =& get_instance();
$joins = 'LEFT JOIN blogrefs ON blogs.id = blogrefs.blog_id';
$joins .= 'LEFT JOIN blogcats ON blogrefs.blogcat_id = blogcats.id';
$category = (isset($params['category']))?$params['category']:'';
$keyword = (isset($params['keyword']))?$params['keyword']:'';
$status = (!isset($params['status']) || $params['status'] == '')?'':$params['status'];
$order_by = (isset($params['order_by']))?$params['order_by']:'created_on';
$direction = (isset($params['direction']))?$params['direction']:'DESC';
$limit = (isset($params['limit']))?$params['limit']:'';
$offset = (isset($params['offset']))?$params['offset']:'';
$st = '';
if($status == ''){
$st = '!';
}
if(!empty($category))
{
$conditions = array(
"((blogcats.id = ?) OR (blogcats.parent_id = ?))
AND blogs.status $st= ?
AND (blogs.title LIKE '%$keyword%' OR blogs.content LIKE '%$keyword%')",$category,$category,$status
);
}else{
$conditions = array(
"blogs.status $st= ?
AND (blogs.title LIKE '%$keyword%' OR blogs.content LIKE '%$keyword%')",$status
);
}
$result = Blog::find('all',array(
'include' => array('blogcat','blogref','user'),
'joins' => $joins,
'conditions' => $conditions,
'limit' => $limit,
'offset' => $offset,
'order' => "$order_by $direction",
'group' => 'blogs.id'
));
$count = Blog::find('all',array(
'conditions' => $conditions
));
$object = new stdClass;
$object->posts = $result;
$object->total = count($count);
return $object;
}
Everything works not only if i use
((blogcats.id = ?) OR (blogcats.parent_id = ?))
To get blogs from specific category. Any answers will be very much appreciated.
I don't find this very clear. Why not use a model and CI's Active Record Class? (http://ellislab.com/codeigniter/user-guide/database/active_record.html), like
$this->db->where('blogrefs.blogcat_id', $catid);
$this->db->join('blogrefs', 'blogs.id = blogrefs.blog_id');
...
Your error is a missing space at the end of where you define the first part of your join.
$joins = 'LEFT JOIN blogrefs ON blogs.id = blogrefs.blog_id';
$joins .= 'LEFT JOIN blogcats ON blogrefs.blogcat_id = blogcats.id';
evaluates to:
LEFT JOIN blogrefs ON blogs.id = blogrefs.blog_idLEFT JOIN blogcats ON blogrefs.blogcat_id = blogcats.id
You'll notice that the first join is screwed up by looking for a column named 'blog_idLEFT' and that the second JOIN will now be an inner join, not an outer left join.
I'd recommend either going with davedriesmans's suggestion, or finding someway to output the final mySQL queries you are generating. (Or both)
I am not able to convert the below query into Zend_Db_Table_Select query.
SELECT GROUP_CONCAT(wallet_transaction_id) as wallet_transaction_ids,transaction_type,source,status
FROM(
SELECT wallet_transaction_id, transaction_type, source, status
FROM ubiw_transactions_wallet
WHERE (game_id = '1292') AND (player_id = 1538)
ORDER BY date DESC LIMIT 100
) a
GROUP BY a.transaction_type,a.status, a.transaction_type;
any help is appreciated.
thanks,
shiv
// Maybe need some changes
$table = new Zend_Db_Table('ubiw_transactions_wallet');
$subSelect = $table->select()
->from('ubiw_transactions_wallet', array('wallet_transaction_id', 'transaction_type', 'source', 'status'))
->where($table->getAdapter()->quoteInto('game_id = ?', 1292))
->where($table->getAdapter()->quoteInto('player_id = ?', 1538))
->order('`date` DESC')
->limit(100);
$mainSelect = $table->select()
->setIntegrityCheck(false)
->from(array('a' => $subSelect), array('wallet_transaction_ids' => new Zend_Db_Expr('GROUP_CONCAT(wallet_transaction_id)'), 'transaction_type', 'source', 'status'))
->group(array('a.transaction_type', 'a.status', 'a.transaction_type'));
$result = $table->fetchAll($select);
I have a query like
select empl.idemp ,(select em.firstnm from tbl_employeemaster em where em.idemp = empl.approvedby) as approvedby from tbl_empleaveapplication empl
join tbl_employeemaster emp on empl.idemp = emp.idemp
join tbl_leavemaster lvm on empl.idleavemaster = lvm.idleavemaster
I need to use zend db to build this type of query
Here is how you can convert your string query into Zend_Db_Select
class Empleaveapplication extends Zend_Db_Table_Abstract
{
protected $_name = 'tbl_empleaveapplication';
}
$table = new Empleaveapplication();
// create sub query
$subSql = $table->select()
->setIntegrityCheck(false)
->from(array('em' => 'tbl_employeemaster'), array('firstnm'))
->where('idemp = empl.approvedby', '');
// main query
$sql = $table->select()
->setIntegrityCheck(false)
->from(array('empl' => 'tbl_empleaveapplication'), array('idemp', 'approvedby' => new Zend_Db_Expr('(' . $subSql . ')')))
->joinInner(array('emp' => 'tbl_employeemaster'), 'empl.idemp = emp.idemp', array())
->joinInner(array('lvm' => 'tbl_leavemaster'), 'empl.idleavemaster = lvm.idleavemaster', array());