How can I execute this SQL query in Drupal? - mysql

I'd like to execute the following SQL query in Drupal...but haven't had any luck.
SELECT SUM(field_count) FROM headon_entry WHERE uid = 1 AND tid = 263;
I've used the following, but with no luck:
$query = db_select('headon_entry', 'he')
->condition('uid', $uid)
->condition('tid', $product_id);
$query->addExpression('SUM(field_count)', 'field_count');
$entry_quantity = $query->execute();

Your query comes out as
SELECT SUM(field_count) AS field_count
FROM
{headon_entry} he
WHERE (uid = :db_condition_placeholder_0) AND (tid = :db_condition_placeholder_1)
So what you've got is correct as far as building the query goes.
What you haven't done is extract the result from the query execution. Try this:
$entry_quantity = $query->execute()->fetchField();

Related

How to filter a query builder with another query?

I'm trying to do next query to the DB to get the next results:
Expected results
I have the table Areas, but I want to sort the results according to the responsible, it means, the responsibles with more areas at the top and the responsibles with less areas at the bottom.
Actually I'm doing it with the next Mysql Query which works perfectly:
SELECT a.id_Area,
a.name,
a.responsible
FROM FSA_Areas a
WHERE enabled=TRUE
AND id_Plant = 1
ORDER BY (SELECT COUNT(*)
FROM FSA_Areas a1
WHERE a1.Responsible = a.Responsible
AND a1.id_Plant = a.id_Plant
AND Enabled='1') DESC,
a.responsible;
But now I want to do it in Doctrine Query Builder or DQL
I've tried this, It didt not work:
$query = $em->createQueryBuilder()
->select('u')
->from('FSABundle:FSAArea','u')
->orderBy($this->createQueryBuilder('u1')
->select('count(u1)')
->from('FSABundle:FSAArea', 'u1')
->where('u1.responsible = u.responsible')
->andWhere('u1.plant = u.plant')
->andWhere('u1.enabled = enabled')
,'DESC')
->getQuery()->getResult();
return $query;
Any Idea about how to the same Mysql Query into DQL or Query Builder?
TEST 1:
$sub = $em->createQueryBuilder('a')
->select('COUNT(a.id)')
->from('FSABundle:FSAArea','a')
->where('a.responsible = u.responsible')
->andWhere('a.plant = u.plant')
->andWhere('a.enabled = :status')
->setParameter('status','true');
$qb->select('u')
->from('FSABundle:FSAArea','u')
->where('u.enabled = :status')
->andWhere('u.plant = :idPlant')
->setParameter('status','enabled')
->setParameter('idPlant','1')
->orderBy($sub->getDQL(),'DESC');
return $qb->getQuery()->getResult();

JOIN two SELECTs with Doctrine

I need to write this query with Doctrine. How can I write it down using QueryBuilder?
SELECT charges.id, charges.currency, charges.total_transactions,
charges.total_volume, charges.commission, refunds.total_payouts
FROM
(SELECT ...very long query...) charges
LEFT JOIN
(SELECT ...very long query...) refunds
ON charges.id = refunds.id AND charges.currency = refunds.currency
You can use Native SQL and map results to entities:
use Doctrine\ORM\Query\ResultSetMapping;
$rsm = new ResultSetMapping;
$rsm->addEntityResult('AppBundle:Charges', 'charges')
->addEntityResult('AppBundle:Refunds', 'refunds')
->addFieldResult('charges', 'id', 'id')
->addFieldResult('charges', 'currency', 'currency')
->addFieldResult('charges', 'total_transactions', 'total_transactions')
->addFieldResult('charges', 'total_volume', 'total_volume')
->addFieldResult('charges', 'commission', 'commission')
->addFieldResult('refunds', 'total_payouts', 'total_payouts')
;
$sql = "
SELECT
charges.id,
charges.currency,
charges.total_transactions,
charges.total_volume,
charges.commission,
refunds.total_payouts
FROM
(SELECT ...very long query...) charges
LEFT JOIN
(SELECT ...very long query...) refunds ON charges.id = refunds.id AND charges.currency = refunds.currency
WHERE some_field = ?
";
$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
$query->setParameter(1, $name);
$entities = $query->getResult();
You can use DQL like this:
$dql = "SELECT ...";
$q = $entityManager->createQuery($dql)->setParameters($arrayParameters);
$result = $q->execute();
or QueryBuilder for each sub-query, like:
// subquery 1
$subQuery1 = $entityManager->createQueryBuilder()
->select('...')
->from('...')
->getDQL()
;
// subquery 2
$subQuery2 = ...
// etc
// ...
// main query
$query = $entityManager->createQueryBuilder()
->select('...')
->from('...', $subQuery1)
->leftJoin('...', $subQuery1->getDQL()),
->where()
;
PS: I just try provide gist for you... hope now you have clue...
Now I found out that it's impossible.
Comment created by stof:
DQL is about querying objects. Supporting subselects in the FROM clause means that the DQL parser is not able to build the result set mapping anymore (as the fields returned by the subquery may not match the object anymore).
This is why it cannot be supported (supporting it only for the case you run the query without the hydration is a no-go IMO as it would mean that the query parsing needs to be dependant of the execution mode).
In your case, the best solution is probably to run a SQL query instead (as you are getting a scalar, you don't need the ORM hydration anyway)
Source: https://github.com/doctrine/doctrine2/issues/3542

Sql Alchemy filter / where clause joined by OR not AND

I want to select a bunch distinct records based off a composite key. In SQL I'd write something like this:
SELECT * FROM security WHERE (
exchange_code = 'exchange_code_1' AND code = 'code_1')
OR (exchange_code = 'exchange_code_2' AND code = 'code_2')
...
OR (exchange_code = 'exchange_code_N' AND code = 'code_N')
)
With SQLAlchemy I'd like to use the filter clause like:
query = sess.query(Security)
[query.filter(
and_(Security.exchange_code == security.exchange_code,
Security.code == security.code)
) for security in securities]
result = query.all()
The problem is filter and where join clauses with an AND not an OR... is there some way to use filter with OR?
Or is my only choice to generate a bunch of individual select's and UNION them? Something like:
first = exchanges.pop()
query = reduce(lambda query, exchange: query.union(exchange.pk_query),
first.pk_query())
query.all()
Use or_:
query = sess.query(Security).filter(
or_(*(and_(Security.exchange_code == security.exchange_code,
Security.code == security.code)
for security in securities)))
If your database supports it, you should use tuple_ instead.

Sub queries in Yii

I have the following mysql query which works well in phpmyadmin when i execute it :
"SELECT * FROM accounts_users WHERE id = ( SELECT teacher_id FROM general_teacher_student_associations WHERE student_id = 509 )";
But when i execute via Yii, it breaks :
$query = "SELECT * FROM accounts_users WHERE id = ( SELECT teacher_id FROM general_teacher_student_associations WHERE student_id =509 )";
$command = Yii::app()->db->createCommand($query);
$teachers_list = $command->query();
return $teachers_list;
509 is a dynamically fetched value.
1. What am i doing wrong?
2. Can this be done in a better way?
/******Edited***********/
Found the error : The sub query returns more than one row. Can i use single query to fetch all the values other than using a foreach loop and then inside that executing another query?
Solution : (Accepting Daniels answer since his comment actually solved the issue)
$query = "SELECT * FROM accounts_users WHERE id IN ( SELECT teacher_id FROM general_teacher_student_associations WHERE student_id =509 )";
$command = Yii::app()->db->createCommand($query);
$teachers_list = $command->queryAll();
return $teachers_list;
p.s: This is an edition work and i am not allowed to touch model and hence using model relations is out of window and thats why i ended up with this
Try:
$teachers_list = Yii::app()->db->createCommand()->select('ausers.*')
->from('accounts_users ausers')
->join('( SELECT teacher_id FROM general_teacher_student_associations WHERE student_id = 509 ) as teachers ON teachers.teacher_id = ausers.id')
->queryRow();

Converting MYSQL to Codeigniter

I am trying to convert a MYSQL query to codeigniter and going no wheres real fast. I am trying to convert this query
$conn->prepare("SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = (SELECT MAX(mix_number) FROM podcasts) order by track asc");
This is in my model:
//$where = '(SELECT MAX(mix_number)from podcasts)';
$this->db->select('id,song,artist,album,track,mix_name,date, link');
//$this->db->where('mix_number', '(SELECT MAX(mix_number)from podcasts)');
$this->db->order_by('track', 'asc');
$query = $this->db->get('podcasts');
return $query->result();
My problem area is in the where statement. When I comment out the where statement I get the data. Obviously not in the manner I want it.
I am doing it this way becuase my next query(s) will be
("SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = **(SELECT MAX(mix_number) FROM podcasts) - 1** order by track asc")
And on down to (SELECT MAX(mix_number) FROM podcasts) - 3
Any thoughts on the proper way of writing the where statement? Thank you for yout time.
Set the third argument of where() to false to prevent CI from altering the string you pass in to the second argument, then you can do the subquery:
return $this->db
->select('id,song,artist,album,track,mix_name,date, link')
->where('mix_number', '(SELECT MAX(mix_number) from podcasts)', false)
->order_by('track', 'asc')
->get('podcasts')
->result();
https://www.codeigniter.com/userguide2/database/active_record.html$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
For me this produces the following query:
SELECT `id`, `song`, `artist`, `album`, `track`, `mix_name`, `date`, `link`
FROM (`podcasts`)
WHERE mix_number = (SELECT MAX(mix_number) from podcasts) ORDER BY `track` asc
If you are not too particular about using CodeIgniter's Active Record syntax, you can simply use your query as is:
$sql = "SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = (SELECT MAX(mix_number) FROM podcasts) order by track asc";
$this->db->query($sql);
and then use $query->result() to get your results.