How can i implement this in Doctrine?
SELECT
(SELECT count(*)
FROM article
JOIN comments ON article.id = comments.article_id
WHERE
date(article.created) = '2015-02-06'
AND
article.id = 1) as a,
(SELECT count(*)
FROM article
JOIN comments ON article.id = comments.article_id
WHERE
date(article.created) = '2015-02-05'
AND
article.id = 1) as b
FROM article
GROUP BY article.id
LIMIT 1
This is what i am trying so far
public function createMyQuery($cdate, $aid){
$parameters = array(
'cdate' => $cdate,
'aid' => $aid,
);
$q = $this->getEntityManager()->createQueryBuilder()
->select(' ')
->addSelect('count(a.id)')
->from('ITJariSocialNetworkBundle:Article', 'a')
->where('article.created', ':cdate'))
->andWhere('article.id = :aid')
setParameters->($parameters)
;
return $q->getQuery();
}
//then i am looping on the calling of the function as following:
for($i=0; $i<7; $i++){
$postDate = date('Y-m-d',strtotime("-" .$i."days"));
$query = $this->createMyQuery(postDate, $aid);
}
$query->execute();
expecting to get the count of all the sub queries
however i am getting Exception error result
i thing the reason is i am doing the query in a wrong way
Related
I have these 2 functions executing 2 different queries:
public function getListPerPage($limit, $offset) {
$dql = "SELECT b FROM Entities\Brain b WHERE b.status = '1' ORDER BY b.id DESC";
$query = $this->getEntityManager()->createQuery($dql);
AND
public function getHiddenItems($limit, $offset, $brain_user_id) {
$dql = "SELECT b FROM Entities\Brain b WHERE b.status = '2'
AND b.user = :brain_user_id ORDER BY b.id DESC";
$query = $this->getEntityManager()->createQuery($dql);
$query->setParameter('brain_user_id', $brain_user_id);
I want to merge them in one function and one query, how can I combine these 2 statements in one query?
I tried using UNION as I read in many other questions and did not work for me.
The way I tried using UNION was:
"(SELECT b FROM Entities\Brain b WHERE b.status = '1')
UNION (SELECT b FROM Entities\Brain b WHERE b.status = '2'
AND b.user = :brain_user_id) ORDER BY b.id DESC";
Gave me an error, maybe my syntax was wrong?
Simply use one model method, store the result of each query on a separate variable and then return an array (or object, your choice) with both results to the controller.
For example:
public function getListAndHiddenItems($limit, $offset, $brain_user_id)
{
$this->load->database();
$this->db->select('fields');
$this->db->from('table_one');
$query = $this->db->get();
$list = $query->result();
$this->db->select('other_fields');
$this->db->from('table_two');
$query = $this->db->get();
$hidden = $query->result();
$result = array('list' => $list, 'hidden' => $hidden);
return $result;
}
Add whatever clauses, joins, etc you need on each query. You would need to only call this single method from the controller and you'll get the two resultsets in different array elements
I've two categories and I want to fetch three records of each category later I found this link UNION query with codeigniter's active record pattern after this I change my DB_Active_rec file and add this code also
var $unions = array();
public function union_push($table = '') {
if ($table != '') {
$this->_track_aliases($table);
$this->from($table);
}
$sql = $this->_compile_select();
array_push($this->unions, $sql);
$this->_reset_select();
}
public function union_flush() {
$this->unions = array();
}
public function union() {
$sql = '(' . implode(') union (', $this->unions) . ')';
$result = $this->query($sql);
$this->union_flush();
return $result;
}
public function union_all() {
$sql = '(' . implode(') union all (', $this->unions) . ')';
$result = $this->query($sql);
$this->union_flush();
return $result;
}
and then I create codeigniter's function based query like this
$this->db->select("*");
$this->db->from("media m");
$this->db->join("category c", "m.category_id=c.id", "INNER");
$this->db->order_by("m.media_files", "DESC");
$this->db->limit(3);
$this->db->union_push();
$this->db->select("*");
$this->db->from("media m");
$this->db->join("category c", "m.category_id=c.id", "INNER");
$this->db->order_by("m.media_files", "DESC");
$this->db->limit(3);
$this->db->union_push();
$getMedia = $this->db->union_all();
create this
(SELECT * FROM media m INNER JOIN category c ON
m.category_id = c.id ORDER BY m.media_files DESC LIMIT 3)
UNION ALL
(SELECT * FROM media m INNER JOIN category c ON
m.category_id = c.id ORDER BY m.media_files DESC LIMIT 3)
Now it is fetching records but not properly I want to use only query, it showing six records first query fetch 3 records and second query fetch three records now records are duplicate I check the id of records it is 6,5,4 and again 6,5,4. It can be done also by PHP but I want to use query. Thanks in advance
I dont know code-igniter, but basicly you want it to do the union first and then apply the order by over the whole set. This would require a subquery. It should result in the following SQL query:
select * from
((SELECT * FROM media m INNER JOIN category c ON m.category_id = c.id )
UNION ALL
(SELECT * FROM media m INNER JOIN category c ON m.category_id = c.id)) T
ORDER BY m.media_files DESC LIMIT 3
Hope it helps you some.
I have a database containing 3 tables (member, boat, boatType) and in the method below two of them is affected. What I want is to list all boats that's registered on a member, but there's a problem with the code (at least with the SQL, but I suspect that's not all).
Thanks in advance.
boatHandler.php:
public function GetMembersBoats($memberId) {
$query = "SELECT b.boatId, b.length, bt.type
FROM boat AS b
INNER JOIN member AS m
ON b.memberId = m.memberId
WHERE m.memberId = ?
INNER JOIN boatType AS bt
ON b.boatTypeId = bt.boatTypeId
GROUP BY b.boatId";
$stmt = $this->m_db->Prepare($query);
$stmt->bind_param('i', $memberId);
$boats = $this->m_db->GetBoats($stmt);
}
database.php:
public function GetBoats($stmt) {
$boats = array(
0 => array(),
1 => array(),
2 => array()
);
$stmt->execute();
$stmt->bind_result($boatId, $length, $type);
while ($stmt->fetch()) {
array_push($boats[0], $boatId);
array_push($boats[1], $length);
array_push($boats[2], $type);
}
$stmt->Close();
return $boats;
}
Affected tables:
boat: boatId, boatTypeId, length, memberId
boatType: boatTypeId, type
The WHERE clause has to be listed after the table references. You have to move the WHERE m.memberId = ? to the end of the table references after the FROM and JOINs like so:
SELECT b.boatId, b.length, bt.type
FROM boat AS b
INNER JOIN member AS m
ON b.memberId = m.memberId
INNER JOIN boatType AS bt
ON b.boatTypeId = bt.boatTypeId
WHERE m.memberId = ?
GROUP BY b.boatId
as you in on clause, simply replace 'where' by 'and':
SELECT b.boatId, b.length, bt.type
FROM boat AS b
INNER JOIN member AS m
ON b.memberId = m.memberId
AND m.memberId = ?
INNER JOIN boatType AS bt
ON b.boatTypeId = bt.boatTypeId
GROUP BY b.boatId
I want to execute below query in zend framework. can anyone tell me how can I do that?
(SELECT `msg`.`message_sender_id`, `msg`.`message_receiver_id`, `msg`.`message_content`, `msg`.`message_sent_on`, `usr`.`user_name` AS `sender_name`
FROM `sc_message` AS `msg` INNER JOIN `sc_user` AS `usr` ON `msg`.`message_sender_id` = `usr`.`user_id`
WHERE `msg`.`message_id` = 3 ORDER BY `msg`.`message_sent_on`)
UNION
(SELECT `msg_slv`.`message_slave_sender_id`, `msg_slv`.`message_slave_receiver_id`, `msg_slv`.`message_slave_content`, `msg_slv`.`message_slave_sent_on`, `usr`.`user_name` AS `sender_name`
FROM `sc_message_slave` AS `msg_slv` INNER JOIN `sc_user` AS `usr` ON `msg_slv`.`message_slave_sender_id` = `usr`.`user_id`
WHERE `msg_slv`.`message_id` = 3 ORDER BY `msg_slv`.`message_slave_sent_on`)
I have written below code
$Query_1 = $this ->select()
->from(array('msg' => 'sc_message'), array('msg.message_sender_id', 'msg.message_receiver_id', 'msg.message_content', 'msg.message_sent_on'))
->joinInner(array('usr' => 'sc_user'), 'msg.message_sender_id = usr.user_id', array('usr.user_name as sender_name'))
->where('msg.message_id = ?',$message_id)
->setIntegrityCheck(false);
$this->_name = "sc_message_slave";
$this->_primary = "message_slave_id";
$Query_2 = $this ->select()
->from(array('msg_slv' => 'sc_message_slave'), array('msg_slv.message_slave_sender_id', 'msg_slv.message_slave_receiver_id','msg_slv.message_slave_content', 'msg_slv.message_slave_sent_on'))
->joinInner(array('usr' => 'sc_user'), 'msg_slv.message_slave_sender_id = usr.user_id', array('usr.user_name as sender_name'))
->where('msg_slv.message_id = ?',$message_id)
->setIntegrityCheck(false);
Assuming you're working from some Zend_Db_Table method
//your code...
$unionSelect = $this->getAdapter()->select()->union(array($Query_1, $Query_2));
//no you can execute it
$rows = $this->getAdapter()->fetchAll($unionSelect);
I have an app that uses the codeigniter CXTags tagging library.
The database structure is as follows:
posts
id
tags_ref
row_id
table
tag_id
tags
id
safe_tag
tag
My query basically goes if $safe_tag is not null then join tags_ref on post.id = tags_ref.row_id, join tags on tags_ref.tag_id = tags.id, where tags_ref.table = 'posts' and tags.safe_tag = 'food'
SELECT * FROM posts
JOIN tags_ref ON posts.id = tags_ref.row_id
JOIN tags ON tags_ref.tag_id = tags.id
WHERE tags.safe_tag = $safe_id
Unfortunately the query I've written in active record is not functioning properly. The query works perfectly when £safe_tag is null but when it's not I get wrong results.
function get_posts($id = NULL, $safe_tag = NULL) {
if($safe_tag != NULL){
echo $safe_tag;//debugging
$table = 'posts';
$this->db->join('tags_ref', 'posts.id = tags_ref.row_id');
$this->db->join('tags', 'tags_ref.tag_id = tags.id');
$this->db->where('tags_ref.table', $table);
$this->db->where('tags.safe_tag',$safe_tag);
}
//if an id was supplied
if ( $id != NULL ) {
$this->db->where('posts.city_id',$id);
}
// execute query
$query = $this->db->get('posts');
...
Here is the query with profiling on:
SELECT *
FROM (`posts`)
INNER JOIN `tags_ref` ON `posts`.`id` = `tags_ref`.`row_id`
INNER JOIN `tags` ON `tags_ref`.`tag_id` = `tags`.`id`
WHERE `tags_ref`.`table` = 'posts'
AND `tags`.`safe_tag` = 'food'
AND `posts`.`city_id` = '2'
Can someone have a look? I think I need a fresh set of eyes on it.
Your forgot to actually run the query inside your first if{}
if($safe_tag != NULL){
echo $safe_tag;//debugging
$table = 'posts';
$this->db->join('tags_ref', 'posts.id = tags_ref.row_id');
$this->db->join('tags', 'tags_ref.tag_id = tags.id');
$this->db->where('tags_ref.table', $table);
$this->db->where('tags.safe_tag',$safe_tag);
$this->db->get(); // here
}