Query Builder Symfony - sql

I tried to translate this sql query into symfony, but, I have not succeeded . Can you help me plz !
In my database :
FichePDF table
( id , ssocc_id, etudiant_id , path)
Evaluation table
(id , ssocc_id, active)
ssocc_id is a primary key for SousOccasion Table.
my query is :
Select * from FichePDF
inner join Evaluation
ON FichePDF.ssocc_id = Evaluation.ssocc_id;
where Evaluation.active= true ;
thank you !

Please try with below query in your FichePDPRepository, May be it will help you.
$this->createQueryBuilder('FichePDP')
->join('Evaluation', 'e')
->where('e.active = :status')
->setParameter('status', true)
->getQuery();

Related

MySQL query to get combination data

First of all, sorry my english is not so good.
my expected result is i want to get data which (residence unit id =21300 and residence user id null) AND (residence_user_id=25106 and residence_unit_id=21300) in one query
PLS HELP ME. thanks!
MySQL Raw query :
SELECT * FROM table_name where residence_unit_id = 21300 AND (residence_user_id = 25106 OR residence_user_id is null);
Laravel eloquent query :
Model::where('residence_unit_id', 21300)->where(function($query) {
$query->where('residence_user_id',25106)>orWhereNull('residence_user_id');
});
Query :
DB::whereNull('residence_user_id')->orWhereIn('residence_user_id' , [25106])->where('residence_unit_id' , 213100)->get()

QueryBuilder - IN expression

I've created a quiz and I record in DB if people answered right to all question and the time they take to finish the quiz.
I'm trying to create a querybuilder to retrieve the guy who answered correct to the maximum of questions with the minimum of time.
My table looks like this :
So, the request (which works) I did in SQL in the DB is :
SELECT
id
FROM
public.user_quizz
WHERE
quizz_id = 4
AND
number_correct_answers IN (SELECT max(number_correct_answers) FROM user_quizz WHERE quizz_id = 4)
AND
answered_in IN (SELECT min(answered_in) FROM user_quizz WHERE quizz_id = 4);
Of course, I don't know if it's the best (and the most optimal) request we could do in this case, but it works.
Now, I'm trying to translate this query into querybuilder.
I'm blocked on the IN expression. I don't know how I could do the SELECT here.
$qb = $this->createQueryBuilder('u');
$query = $qb->select('u')
->andWhere(
$qb->expr()->eq('u.quizz', ':quizzId'),
$qb->expr()->in(
'u.numberCorrectAnswers',
)
)
->setParameter('quizzId', $quizz->getId())
->getQuery()
;
Thanks for your help.
$qbSelectMax = $this->createQueryBuilder('uc') // user copy, to prevent alias collisions
$qbSelectMax
->select($qb->expr()->max('uc.numberCorrectAnswers'))
->where($qb->expr()->eq('uc.quizz', ':quizzId'));
$qb = $this->createQueryBuilder('u')
$query = $qb->select('u')
->andWhere(
$qb->expr()->eq('u.quizz', ':quizzId'),
$qb->expr()->in(
'u.numberCorrectAnswers',
$qbSelectMax->getDQL()
)
)
->setParameter('quizzId', $quizz->getId())
->getQuery();
You can create sub DQL query to select max numberCorrectAnswers first and then pass DQL right into in parameter
ORDER BY could be used to sort by number of answers and less time taken:
$qb = $this->createQueryBuilder('u')
->orderBy('u.numberCorrectAnswers', 'DESC')
->addOrderBy('u.answeredIn', 'ASC');

SQL SELECT Query Issues

I am having an issue with my SELECT query. I am trying to find items that do not start with "50700" and have an enabled flag of 1, are in category 4 and sub category 4. Below is the query i have but it is not returning any results when i know there are some. Please note TBL1.FIELD1 = ITM_ID, TBL1.FIELD2 = ENABLED, TBL2.FIELD1 = CAT_ID, TBL2.FIELD2 = SUBCAT_ID.
SELECT DB_NAME.dbo.TBL1.FIELD1
, DB_NAME.dbo.TBL1.FIELD2
, DB_NAME.dbo.TBL2.FIELD1
, DB_NAME.dbo.TBL2.FIELD2
FROM DB_NAME.dbo.TBL1
, DB_NAME.dbo.TBL2
WHERE DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700%'
AND DB_NAME.dbo.TBL1.FIELD2 = 1
AND DB_NAME.dbo.TBL2.FIELD1 = 4
AND DB_NAME.dbo.TBL2.FIELD2 = 4
You can break the query into different subqueries for debug purpose, this way you can figure out which "AND" condition is not working.
For Example:
Firstly I guess you should try a simple query on TBL1.FIELD1 with the simple query :
select TBL1.FIELD1 from DB_NAME.dbo.TBL1 where DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700' ;(*if TBL1.FIELD1 is of String type, else go for TBL1.FIELD1 != 50700 * )
Then if you get results ,try to do "AND" with the TBL1.FIELD2 condition.
select TBL1.FIELD1
from DB_NAME.dbo.TBL1
where DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700'
and DB_NAME.dbo.TBL1.FIELD2 =4 ;
This way you can proceed further and debug the query. Hope it helps :)

Select distinct value count laravel

I have an orders table where there is a status column.
I am trying to get the count of each status like this :
$pending = Order::where('status','=','pending')->count();
$active = Order::where('status','=','active')->count();
But this is not efficient as am having to make one call for each status type.
How can I reduce number of queries ?
To make an SQL request:
SELECT count(DISTINCT `status_id`) FROM `dbname`.orders WHERE `user_id` = '2';
In Laravel you can try the following:
$user->orders()->distinct()->count(["status_id"]);
You could try
$orders = Order::select(DB::raw('count(*) as order_count, status'))
->groupBy('status')
->get();
here is the way you write ->
$users= DB::table('table_name')->distinct()->get(['column_name']);
$users_count = $users->count();

Convert PostgreSQL to SqlAlchemy query

I have a simple query in PostgreSQL which I need to convert to SQLAlchemy.
SELECT table1.name,table2.ip,table2.info
FROM table1 LEFT OUTER JOIN table2
ON(table1.name=table2.hostname)
GROUP BY table1.name;
I have tried using this with no success:
session.query(table1.name
,table2.ip
,table2.info).distinct().join(table2).group_by(table1.name)
Can someone please help me with this?
Thanks in advance
Ishwar
Here's how you would do it using the Expression API
http://docs.sqlalchemy.org/en/latest/core/expression_api.html
Define your table, for example:
table1 = table('table1',
column('name', String())
)
table2 = table('table2',
column('ip', String()),
column('info', String()),
column('hostname', String()),
)
Compose your query, like:
my_query = select([table1.c.name,
table2.c.ip,
table2.c.info]
).select_from(
table1.outerjoin(table2, table1.c.name == table2.c.hostname)
).group_by(table1.c.name)
Execute your query, like:
db.execute(my_query)
It's not clear from your answer what the problem is, but my shoot at this query would be:
session.query(table1.name
,table2.ip
,table2.info)\
.outerjoin(table2)\
.group_by(table1.name)
I figured out the answer. I was actually missing the ON condition in query:
session.query(table1.name
,table2.ip
,table2.info
).distinct().outerjoin(table2 , table1.name=table2.hostname
).group_by(table1.name)
Thanks for help!