I really need help here, does anyone know how to transform this SQL query into a Doctrine2 query using the createQueryBuilder?
SELECT a.resposta,
(
SELECT count(r.id)
FROM car_resultado r2
LEFT JOIN car_resultado_inquerito ri2 ON r2.id_resultado_inquerito = ri2.id
WHERE ri2.id_inquerito = 20 AND r2.id_resposta = a.id
GROUP BY r2.id_pergunta, r2.id_resposta
) as total
FROM car_resposta a
LEFT JOIN car_resultado r ON ( r.id_resposta = a.id )
GROUP BY a.id, r.id_resposta
I have no idea how to do it, mainly because that nested SELECT
Normally you have to create your Entities and Repositories to get the data from your database. But in Doctrine2 is a uncool way. You can execute native SQL.
Doctrine2 Native SQL
If its possible you shouldn't do it and work with the classes but if you have a complicated existing queries you can do it.
Related
I've got a little issue. I've got to do a query that is based on a subquery.
The SQL query I need to get to is:
FROM testing t
WHERE t.id NOT IN (
SELECT h.task_id
FROM task t
INNER JOIN hardware h ON h.task_id = t.id
);
What I have for a moment is the basic query and I don't really understand how to do that subquery..
$test->createQueryBuilder('t')
->select('t.id')
->getQuery()->getArrayResult();
I am working on a query with the following format:
I require all the columns from the Database 'A', while I only require the summed amount (sum(amount)) from the Database 'B'.
SELECT A.*, sum(B.CURTRXAM) as 'Current Transaction Amt'
FROM A
LEFT JOIN C
ON A.Schedule_Number = C.Schedule_Number
LEFT JOIN B
ON A.DOCNUMBR = B.DOCNUMBR
ON A.CUSTNMBR = B.CUSTNMBR
GROUP BY A
ORDER BY A.CUSTNMBR
My question is regarding the grouping statement, database A has about 12 columns and to group by each individually is tedious, is there a cleaner way to do this such as:
GROUP BY A
I am not sure if a simpler way exists as I am new to SQL, I have previously investigated GROUPING_ID statements but thats about it.
Any help on lumped methods of grouping would be helpful
Since the docnumber is the primary key - just use the following SQL:
SELECT A.*, sum(B.CURTRXAM) as 'Current Transaction Amt'
FROM A
LEFT JOIN C
ON A.Schedule_Number = C.Schedule_Number
LEFT JOIN B
ON A.DOCNUMBR = B.DOCNUMBR
ORDER BY RM20401.CUSTNMBR
GROUP BY A.DOCNUMBR
I have two models Channel and Program. The relationship is : A Channel has many Programs.
Now I intend to show how many programs a specific channel has in my view.
So I come up with the following SQL to fetch relevant data.
SELECT channels.channel_id, channels.name, num_of_programs
FROM channels
LEFT JOIN
(
SELECT p.channel_id cid, COUNT(*) num_of_programs
FROM programs p, channels c
WHERE p.channel_id = c.channel_id
GROUP BY p.channel_id
) v
ON channels.channel_id = v.cid;
How to write this query in Laravel Eloquent?
By the way, are there better,more efficient SQL to accomplish my task?
Thank you in advance!
There are so many possibilities. That's one:
DB::table('channels')
->join('programs', 'channels.id', '=', 'programs.channel_id')
->select('channels.id', 'channles.name', DB::raw('count(programs.id) as num_of_programs'))
->groupBy('channels.id', 'channels.name')
->get();
I hope it works fine for you.
You can accomplish this with the join and group by, and i am not familiar with Laravel Eloquent.
You can try with this
SELECT c.channel_id,c.name,COUNT(p.program_id)
FROM channels c
LEFT JOIN programs p ON p.channel_id = c.channel_id
GROUP BY p.channel_id;
I have a bulk query with subquery. My query works fine when I run it on development server, but when I've try it pn the live server, the query takes too much time to produce an output. I think it's because of a big data on the live server. Can anyone help me on how to index query on MySQL so that it will lessen the time execution.
Here is my query:
SELECT prd.fldemployeeno AS Empno,
(SELECT fldemployeename FROM tblprofile prf WHERE prf.fldemployeeno = prd.fldemployeeno LIMIT 0,1) AS Empname,
'01' AS `Week`,
COUNT(DISTINCT isAud.fldid) AuditedFiles,
COUNT(qua.seqid) ErrorCount,
COUNT(DISTINCT qua.fldid) OrdersWithError
FROM tbldownloadITL dwn
INNER JOIN tblproductionITL prd
ON dwn.fldid = prd.fldglobalid
INNER JOIN (SELECT p.fldemployeeno,fldglobalid,p.fldstarttime,COALESCE(q.fldstarttime,p.fldstarttime) `AuditDate`
FROM tblproductionitl p
LEFT JOIN tblqualityaudit q
ON p.fldemployeeno=q.fldemployeeno
AND p.fldstarttime=q.fldprodstarttime
AND p.fldglobalid=q.fldid
WHERE p.fldprojectgroup='PROJGROUP') temp
ON prd.fldglobalid=temp.fldglobalid
AND prd.fldemployeeno=temp.fldemployeeno
AND prd.fldstarttime=temp.fldstarttime
INNER JOIN tblisauditedITL isAud
USING (fldid)
LEFT JOIN tblqualityaudit qua
ON qua.fldid = dwn.fldid
AND qua.fldbusunit = dwn.fldbusunit
AND qua.fldprojectGroup = dwn.fldprojectGroup
AND qua.fldemployeeno = prd.fldemployeeno
AND qua.fldprodstarttime = prd.fldstarttime
AND qua.flderrorstatus != 'NOT ERROR'
LEFT JOIN tblerrorcategory
USING (flderrorcategoryid)
LEFT JOIN tblerrortypes
USING (flderrortypeid)
WHERE dwn.fldbusunit = 'BUSUNIT'
AND dwn.fldprojectGroup = 'PROJGROUP'
AND temp.AuditDate BETWEEN '2011-07-29 00:00:00' AND '2011-07-29 23:59:59'
GROUP BY prd.fldemployeeno
ORDER BY Empname
Here is also the description of the query:
I would suggest installing Sphinx on the your server if you have the access. That way you can have an indexed resource at your finger tips for extremely fast searching, on top of that you can add the execution of what is called a 'delta' index to allow for real time updating of your mysql database. It is highly customizable. Hopefully this will help you out.
http://sphinxsearch.com/
I'm converting my database from SQL Server to MS Access.
This query works fine in SQL Server, but it has syntax error in MS-Access.
So what is wrong with this SQL statement??
select *
from Students
left join (select Lessons.StudentID, COUNT(*) as LessonsCount
from Lessons
group by Lessons.StudentID) as c on c.StudentID = Students.ID
left join (select Tests.StudentID, count(*) as TestsCount
from Tests
group by Tests.StudentID) as dd on dd.StudentID = c.StudentID
Access SQL requires parentheses with multiple joins. The fail-safe way to deal with that issue is to set up your joins in the query designer, and let Access add the parentheses it wants.
It may come out looking something like this:
select *
from (Students
left join [select Lessons.StudentID, COUNT(*) as LessonsCount
from Lessons
group by Lessons.StudentID]. as c on c.StudentID = Students.ID)
left join [select Tests.StudentID, count(*) as TestsCount
from Tests
group by Tests.StudentID]. as dd on dd.StudentID = c.StudentID
A side effect with the query designer is that it tends to convert subqueries from this form
(SELECT some_field FROM some_table) AS sub
to this ...
[SELECT some_field FROM some_table]. AS sub