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
Related
As the title says, the MySQL Workbench is reporting an error. The error is from the software, not from MySQL, as far as I can tell. However, it's preventing me from bug fixing the code, as it will not work on my website.
Originally I used this query, which works perfectly fine, but is very slow, as it has a select inside the inner join.
SELECT *
FROM modeller AS m
INNER JOIN
bilder AS b ON b.ID = (
SELECT ID FROM bilder AS b2
WHERE b2.modellID = m.id
ORDER BY filnavn
DESC LIMIT 1
)
order by m.fornavn
So I've been looking around for a replacement and ended up with this:
SELECT *
FROM (
SELECT p.*,
m.*,
row_number() over (partition by m.id order by filnavn desc) rono
FROM modeller AS m
INNER JOIN bilder AS B
on B.modellID= m.ID
) x
WHERE x.rono = 1
What this query is supposed to do it to look through a database of 3D models ('modeller') and get the latest picture added to that model in the table 'bilder'. This is a one-to-many connection, bilder has modellID as a column, which corresponds to the ID in modeller.
Instead, I get the error
SELECT is not valid at this position for this server version, expecting : '(', WIDTH
Any tips hints or suggestions are greatly appreciated, especially something that not only circumvents the Workbench-error, but also improves on my initial query.
An alternative you could try is:
SELECT *
FROM modeller AS m
INNER JOIN (
SELECT model1ID, MAX(filnavn) AS filnavnMax
FROM bilder
GROUP BY model1ID
) AS maxVals
ON m.id = maxVals.model1ID
INNER JOIN bilder AS b
ON maxVals.model1ID = b.model1ID AND maxVals.filnavnMax = b.filnavn
ORDER BY m.fornavn
;
However, if filnavn is not unique for a model1ID, you could end up with more results than your original query returned. But if that is the case, your original query could provide inconsistent results (since there is no guarantee the ID returned from the original subquery would be consistent when there is more than one row with the highest filnavn)
I am working on migrating from Oracle to MySQL and currently stuck on converting query with MINUS operation. I know, I have to use left join, but somehow my query is not giving the exact rows as it is in Oracle. The query is very simple with with select on views "V_*".
On Oracle:
select s.section, c.title from course c, v_staff_active s
minus
select section,title from v_rep_attended_by_section
On Mysql, I converted it like below:
select * from
( select s.section, c.title from course c, v_staff_active s) x
left join
( select section,title from v_rep_attended_by_section) y on x.section = y.section
where y.section is null;
But not getting the exact result or number of records.
Can you please help me out as I am very much new to MySQL.
Thanks in advance.
Your query should look like:
select s.section, c.title
from course c
left join v_staff_active s on c.section = s.section
and c.title = s.title
where s.section is null and s.title is null;
I created a view using MySql subquery, it worked on my localhost,
it failed in production because Mysql version is 5.6, it doesn't support subqueries on views, so i need help rewriting the whole query,
I tried with UNION ALL but it tells me i need the same amount of columns for each Select.
New query:
CREATE VIEW vista_inventarionacional AS
SELECT T.Ultima_Actualizacion,
r.nombre AS Red,
co.nombre AS Comuna,
equipo.nombre AS Descripcion,
equipo.marca AS Marca,
equipo.modelo AS Modelo,
p.nombre AS Parametro,
equipo.nserie AS NumSerie,
equipo.fecha_compra AS Fecha_Recepcion,
equipo.id_mma AS MMA,
equipo.fecha_inicio_uso AS FechaInstalacion,
equipo.fecha_vigencia as FechaBaja,
e.nombre AS LugarInstalacion,
eq_etd.nombre AS EstadoEquipo
FROM equipo
LEFT JOIN equipo_estacion AS eq_est ON eq_est.equipo_id = equipo.id
JOIN estacion AS e ON e.id = eq_est.estacion_id
LEFT JOIN red AS r ON r.id = e.red_id
LEFT JOIN comuna AS co ON co.id = e.comuna_id
LEFT JOIN equipo_parametro AS eq_p ON eq_p.equipo_id = equipo.id
JOIN parametro AS p ON p.id = eq_p.parametro_id
LEFT JOIN equipo_estado AS eq_etd ON eq_etd.id = equipo.equipo_estado_id
WHERE eq_est.estado = 'activo';
UNION ALL
SELECT equipo_id, MAX(fecha) AS Ultima_Actualizacion
FROM transferencia GROUP BY equipo_id
Old Query with Subquery:
Thanks in advance!
Your query:
UNION ALL
SELECT equipo_id, MAX(fecha) AS Ultima_Actualizacion
FROM transferencia GROUP BY equipo_id
is incorrect. Because in 1st query you have 14 column you try to union all with 2 column.
You can use :
SELECT equipo_id, MAX(fecha), null,null,null,null,null,null,null,null,null,null,null,null, AS Ultima_Actualizacion
FROM transferencia GROUP BY equipo_id;
Also View's select contains a subquery error: You can solve this. Use mysql workbrench to connect mysql DB. and run create view script from mysqlworkbrench.
It will work.
So I have checked all the names etc are correct, the query works with only one of the joins not both with a syntax error:
SELECT *, Invoice_Accounts.Title AS t1, Invoice_Accounts.Forename AS f1, Invoice_Accounts.Surname AS s1
FROM Invoice_Accounts
RIGHT JOIN Delivery_Points ON Invoice_Accounts.Account_No = Delivery_Points.Account_No
RIGHT JOIN Companies ON Invoice_Accounts.account_id = Companies.Company_No
This is the query I want to run but it comes up with the error:
syntax error (missing operator)
UPDATE:
using:
SELECT *,
Invoice_Accounts.Title AS t1,
Invoice_Accounts.Forename AS f1,
Invoice_Accounts.Surname AS s1
FROM (Invoice_Accounts
RIGHT JOIN Delivery_Points
ON Invoice_Accounts.Account_No = Delivery_Points.Account_No)
RIGHT JOIN Companies
ON Invoice_Accounts.account_id = Companies.Company_No
Gets me the error "JOIN expression not supported"
In Access you need to use parenthesis around joins if you have more than one:
SELECT *,
Invoice_Accounts.Title AS t1,
Invoice_Accounts.Forename AS f1,
Invoice_Accounts.Surname AS s1
FROM (Invoice_Accounts
RIGHT JOIN Delivery_Points
ON Invoice_Accounts.Account_No = Delivery_Points.Account_No)
RIGHT JOIN Companies
ON Invoice_Accounts.account_id = Companies.Company_No
Two extend this, if you had three joins, you would need another set of parentheses:
SELECT *
FROM (( A
INNER JOIN B
ON B.AID = A.AID)
INNER JOIN C
ON C.BID = B.BID)
INNER JOIN D
ON D.CID = C.CID;
EDIT
I did not know that you could not RIGHT JOIN to the same table twice, so the above is an error, having said that I have never used a RIGHT JOIN in production code in my life, I would be inclined to switch to the more widely used LEFT JOIN and change the order of the tables:
SELECT *,
ia.Title AS t1,
ia.Forename AS f1,
ia.Surname AS s1
FROM (Companies AS c
LEFT JOIN Invoice_Accounts AS ia
ON ia.account_id = c.Company_No)
LEFT JOIN Delivery_Points AS dp
ON ia.Account_No = dp.Account_No;
N.B. I have used aliases to reduce the amount of text in the query, this (in my opinion) makes them easier to read
Figured out the answer eventually:
SELECT Invoice_Accounts.Title AS t1, Invoice_Accounts.Forename AS f1, Invoice_Accounts.Surname AS s1, *
FROM (Companies
RIGHT JOIN Invoice_Accounts ON Companies.Company_No = Invoice_Accounts.Company_Name
)
RIGHT JOIN Delivery_Points ON Invoice_Accounts.Account_No = Delivery_Points.Account_No;
The issue arises from ancient ms-access technologies where you can't right join to the same table more than once!
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.