Write nested Select queries for same table in Zend Framework2 - php

Following is my query
SELECT d.[today date], d.mydatediff,d.cc_exp
FROM (
SELECT GETDATE() AS 'today date',
cc_exp,
DATEDIFF(DAY, CONVERT(DATE, DATEADD(DAY, -1, DATEADD(MONTH, 1, CONVERT(DATETIME, '1/' + cc_exp, 103)))), GETDATE()) AS mydatediff,
[customer_member_id]
FROM [ats].[dbo].[customers]
WHERE cc_exp IS NOT NULL AND customer_member_id IN ('11111','2222','33333')
) d
WHERE d.mydatediff BETWEEN 30 AND 100
ORDER BY d.mydatediff DESC
I tried creating two SQL statments
$column = array(
'dateDiff' => new Expression('DATEDIFF(DAY, CONVERT(DATE, DATEADD(DAY, -1, DATEADD(MONTH, 1, CONVERT(DATETIME, \'1/\' + cc_exp, 103)))), GETDATE())'),
'cc_exp',
'customer_member_id'
);
$subselect = $sql->select();
$subselect->from(array('cus' => 'customers'));
$subselect->columns($column);
$where = new Where();
$where->in('customer_member_id', $memberId);
$where->and->isNotNull('cc_exp');
$subselect->where($where);
//$parent select
$select = $sql->select();
$select->from(array($subselect));
$selectColumn = array('cus.customer_member_id', 'cus.cc_exp', 'cus.dateDiff');
$where2 = new Where();
$where->between('cus.dateDiff','30','100');
$select->where($where2);
But it gives me an error
from() expects $table as an array is a single element associative array

You have a number of errors in this code that I can see:
You're defining the cus alias inside the subquery, but then using it in the main query
You're defining a list of columns for the main query but then not using them
You're not defining an alias for the subquery results
Try refactoring the code using the same aliases used in the original query, this will make it easier to compare the two. Then I think your main query would have something like this $select->from(array("d" => $subselect)); which should work.

Related

How to correctly prepare parameters with DoctrineDBAL on the following query?

I need to get the percentage of each possible values in the field column, over the total value of my table.
I found two way to get my result in SQL:
SELECT m.field, sum(m.value) * 100 / t.total
FROM my_table AS m
CROSS JOIN (
SELECT SUM(value) AS total FROM
WHERE year = 2000) t
WHERE m.year = 2000
GROUP BY m.field, t.total
And
SELECT m.field, sum(m.value) * 100 / (SELECT SUM(value) AS total FROM WHERE year = 2000)
FROM my_table AS m
WHERE m.year = 2000
GROUP BY m.field
But both are nested queries, and I don't know how to prepare statments with the Doctrine's QueryBuilder into a nested queries.
Is there a way to do it?
I have been trying to do so using querybuilder and DQL with no success. As it seems, DQL doesn't allow operations with subqueries in SELECT. What I've achieved so far:
$subQuery = $em->createQueryBuilder('m')
->select("SUM(m.value)")
->where("m.year = 2000")
->getDQL();
The following query works though doesn't calculate the percentage:
$query = $em->createQueryBuilder('f')
->select("f.field")
->addSelect(sprintf('(%s) AS total', $subQuery))
->addSelect('(SUM(f.value)*100) AS percentage')
->where("f.year = 2000")
->groupBy("f.field")
->getQuery()
->getResult();
However, if you try to add the division in the select in order to get the percentage and you use the subquery, it simply doesn't work. Looks like the construction it's not allowed in DQL. I've tried with an alias and with the subquery directly and neither of them worked.
Doesn't work:
$query = $em->createQueryBuilder('f')
->select("f.field")
->addSelect(sprintf('(%s) AS total', $subQuery))
->addSelect('(SUM(f.value)*100)/total AS percentage')
->where("f.year = 2000")
->groupBy("f.field")
->getQuery()
->getResult();
Doesn't work either:
$query = $em->createQueryBuilder('f')
->select("f.field")
->addSelect(sprintf('(SUM(f.value)*100)/(%s) AS percentage', $subQuery))
->where("f.year = 2000")
->groupBy("f.field")
->getQuery()
->getResult();
I'd suggest using SQL directly (Doctrine allows it). Using native sql queries and mapping the results would do the trick. There is no disadvantage in doing so.
Documentation
If you find a way of doing it using queryBuilder or DQL, please let me know.
Hope it helps.
yeah! the solution is:
$qs = $this
->createQueryBuilder('h');
$d = $qs ->select($qs->expr()->count('h'));
$e = $d->getQuery()->getScalarResult();
$qs->addSelect('(COUNT(h.id)*100 / :t) AS percentage')->setParameter('t', $e);
$qs->addGroupBy(sprintf('h.%s', $type));
return $qs->getQuery()->getResult();

Laravel 4 union generate wrong SQL

Here is the code:
$f = DB::table("topics")
->join("recommends", "topics.id", "=", "recommends.courseid")
->where("recommends.re_type", "=", $re_type)
->where("recommends.re_location", "=", $re_location)
->orderBy("recommends.weigh", "desc");
$s = DB::table("topics")
->orderBy("topics.create_time", "desc");
$f->union($s)->get();
I got a wrong SQL around key word union:
select * from `topics` inner join `recommends`
on `topics`.`id` = `recommends`.`courseid`
where `recommends`.`re_type` = ?
and `recommends`.`re_location` = ?
order by `recommends`.`weigh` desc
union //here!!!!!
select * from `topics` order by `topics`.`create_time` desc
The error info:
SQLSTATE[HY000]: General error: 1221
Incorrect usage of UNION and ORDER BY (SQL: ...)
(Bindings: array ( 0 => 3, 1 => 7, ))
What is the problem?
MySQL UNIONs expect the same columns in all statements. Because you're joining another table in $f, the columns between the two statements don't match.
See MySql SELECT union for different columns?
In this case, it might be less of a headache to use the PDO object directly.
$pdo = DB::connection()->getPdo();
Found another problem with your query. You should relocate your first order by clause:
->orderBy("recommends.weigh", "desc");
It is producing the order by you before union and MySQL will not accept that.

Multiple SQL queries and FOREACH - Can be JOINED to one?

I am trying to pull a list of Events, also seeing which members have paid for the Events. I then want to see if they are on the committee, to see if they have admin permissions.
I have successfully done this, using three SQL queries, then using three foreach loops to build the Array.
I am SURE this can be done with one SQL query and one foreach loop, however I have not yet mastered the JOIN technique.
I am using Expression Engine, Codeigniter Active Record, I will display to you the SQL output and also what my current EE functions look like.
THANKS FOR THE HELP! :D
SQL to select ALL events which are active
SELECT `id` as event_ID, `name` as event_name, `description` as event_description
FROM (`events`)
WHERE `events_category_id` = '1'
AND `active` = 1
ORDER BY `name` asc
EE CODE to achieve this:
$query = $this->theDb->select('id as event_ID, name as event_name, description as event_description')
->order_by("name", "asc")
->get_where('events', array('event_category_id'=>$event_type,'active'=>1));
**
SQL to find what EVENT IDs the user has paid for
**
SELECT DISTINCT `products`.`event_ID` as joinedID
FROM (`transactions_items`)
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`
JOIN `events` ON `events`.`id` = `products`.`event_ID`
WHERE `transactions`.`member_id` = 27500
AND `events`.`active` = 1
AND `event_category_id` = '1'
ORDER BY `events`.`name` asc
EE CODE to achieve this
$query = $this->theDb->select('products.event_ID as joinedID')
->distinct()
->order_by("events.name", "asc")
->join('transactions', 'transactions.id = transactions_items.id')
->join('products', 'products.id = transactions_items.product_id')
->join('events', 'events.id = products.event_ID')
->get_where('transactions_items', array('transactions.member_id' => $memberID, 'events.active' => 1,'activity_category_id'=>$activity_type));
SQL to find ADMIN rights
SELECT `events`.`id` as event_ID, `admins`.`admin_role_id` as role_id, `admins_roles`.`name` as role_description
FROM (`admins`)
JOIN `admins_roles` ON `admins`.`admin_role_id` = `admins_roles`.`id`
JOIN `events` ON `events`.`id` = `admins`.`event_ID`
WHERE `admins`.`member_id` = 27500
AND `events`.`active` = 1
EE CODE to achieve this
$query = $this->theDb->select('events.id as event_ID, admins.admin_role_id as role_id, admins_roles.name as role_description')
->join('admins_roles', 'admins.admin_role_id = admins_roles.id')
->join('events', 'events.id = admins.event_ID')
->get_where('admins', array('admins.member_id' => $memberID, 'events.active' => 1));
FOR EACH LOOPS
// Create list of Events setting defaults
foreach($events_list as $row)
{
$combinedEvents[$row->event_ID] = array(
'eventID' => $row->event_ID,
'eventName' => $row->event_name,
'eventDescription' => $row->event_description,
'isJoined' => 0,
'roleID' => 0,
'roleDescription' => "",
);
}
// Add Committee roles
foreach($admin_list as $row)
{
$combinedEvents[$row->event_ID]['roleID'] = $row->role_id;
$combinedEvents[$row->event_ID]['roleDescription'] = $row->role_description;
}
// Add Transactions
foreach($transaction_list as $row)
{
$combinedEvents[$row->joinedID]['isJoined'] = 1;
}
I don't quite understand the FOREACH part because I've never touched PHP - but you should be able to solve the multiple SQL queires using the ;with clause. I have created an example in response to another question here and here. Is this what you're looking for?

How to implement sql query in zend db

I tried myself for another queries but this one is more complex for me as i am new to zend. Please help me i tried different ways but not worked.
Tour Id fetching from another query
$tourId = $row2 ['test_public_id'];
$query = select count(ms.test_public_id) as total_views, ms1.recent_views from test_stats
ms join (select count(test_stats.test_public_id) as recent_views
from test_stats where test_stats.test_public_id = '$tourId'
and test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)) ms1
where ms.test_public_id ='$tourId'" ;
Something like that should work:
$subselect = $dbAdapther->select()->from(
array('test_stats' => 'test_stats'),
array(
'(COUNT(test_public_id)) AS recent_views'
)
)->where(
$dbAdapther->quoteInto('test_stats.test_public_id = ?', $tourId)
)->where(
'test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)'
);
$select = $dbAdapther->select()->from(
array('ms' => 'test_stats'),
array(
'(COUNT(ms.test_public_id)) AS total_views' // COUNT should be in brackets to preevent Zend from interpreting it as a field name
)
)->join(
array('ms1' => $subselect),
'',
array(
'ms1.recent_views'
)
)->where(
$dbAdapther->quoteInto('ms.test_public_id = ?', $tourId)'
);
Although I'd have your query broken into two separate ones or, more precisely, write a universal "get number of views" query with a date as its parameter, and then I'd be calling it twice, with or without the date.
But if you still need to get those two figures in one go in a single row (i.e. you can't use UNION instead of your unnecessary JOIN), I'd recommend you to use the following code instead:
$select = $dbAdapther->select()->from(
array('ms' => 'test_stats'),
array(
'(COUNT(ms.test_public_id)) AS total_views',
'(
COUNT(
CASE
WHEN ms.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)) THEN ms.test_public_id
ELSE NULL
END
)
) AS recent_views'
)
)->where(
$dbAdapther->quoteInto('ms.test_public_id = ?', $tourId)
);
I'm new too in Zend, but I've tried this sample and it's works.
See this tutorial, I hope it'll help you:
http://framework.zend.com/manual/en/zend.db.select.html
or you can do this:
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$stmt = $db->query($query);
$result = $stmt->fetchAll();

how to convert propel criteria in symfony

select a.id, b.title, b.start_time, b.end_time from tv_channel a
left join tv_program b on a.id = b.tv_channel_id and b.start_time >= ‘2011-09-23 12:00:00′ and b.end_time <= '2011-09-23 14:30:00'
order by a.code
limit 0, 10;
–pager object
tnx
What's the question ? Do you want to write this SQL query in Propel ?
<?php
TvChannelQuery::create('a')
->joinTvProgram('b')
->addJoinCondition('b', 'b.StartTime >= 2011-09-23 12:00:00')
->addJoinCondition('b', 'b.EndTime >= 2011-09-23 14:30:00')
->orderByCode()
->limit(10)
;
Something like that should work but be be careful about values passed in addJoinCondition, there is no binding here and if you want to change these values you have to use it to prevent SQL injections or other security issues.
$c = new Criteria();
$c->addLeftJoin(tv_channel.id = tv_program .channel_id);
$c->add(tv_program.start_time, '2011-09-23 12:00:00', Criteria::GREATER_EQUAL);
$c->add(tv_program.end_time, '2011-09-23 14:30:00', Criteria::LESS_EQUAL);
$c->addAscendingOrderByColumn(tv_channel.code);
$c->setLimit(10);
$rs = DoSelect($c); //actual select execution here...

Categories