Can someone help me I've got 2 MySQL queries that get unknown column id when I try to run them. I might add that I am converting this database from SQLServer 2005 to MySQL and they run fine in SQL Server 2005.
Here's 1 of them:
SELECT DISTINCT g.id AS `genre`
FROM media_playlist_sequence MPS
INNER JOIN media M ON M.`key` = MPS.media_key
INNER JOIN media_playlists MP ON MP.`key` = MPS.playlist_key
INNER JOIN node_media_playlist NMP ON NMP.playlist_key = MP.`key`
INNER JOIN nodes N ON N.`key` = NMP.node_key
INNER JOIN media_files MF ON MF.media_key = M.`key`
INNER JOIN media_locations ML ON ML.media_file_key = MF.media_file_key
AND ML.node_key = n.`key`
INNER JOIN media_genres MG ON MG.media_key = M.`key`
INNER JOIN genres G ON G.`key` = MG.genre_key
WHERE M.is_ready = 1
AND MP.id = 'Channels'
AND N.id = 'VIC-WIN7'
AND mf.is_quad_image = 0
My guess is that it's a case sensitivity issue. MySQL can be case sensitive by default whereas SQL Server is not.
Related
The first request make my server crash (The CPU go to 100%)
SELECT s.idSinistre
FROM test_apc_sinistres s
INNER JOIN apc_correspondances c ON c.idLiaison = s.idSinistre AND c.refCategorie = '69.1'
INNER JOIN apc_procedures_taches_sinistres p ON s.idSinistre = p.idSinistre
INNER JOIN apc_contacts co ON s.idAdb = co.idContact
INNER JOIN apc_parametres_adb pa ON pa.idAdb = co.idContact
WHERE s.refStatut = '62.2'
GROUP BY s.idSinistre;
select co.Nom, s.idSinistre, count(c.idMessage) as nbMEssage, count(p.id) as nbProc
from test_apc_sinistres s
inner join apc_correspondances c on c.idLiaison = s.idSinistre and c.refCategorie = '69.1'
inner join apc_procedures_taches_sinistres p on s.idSinistre = p.idSinistre
inner join apc_contacts co on s.idAdb = co.idContact
inner join apc_parametres_adb pa on pa.idAdb = co.idContact
where s.refStatut = '62.2'
group by s.idSinistre;
The only difference between this two is the data i select. Someone already have this issue?
Just re-check your columns one by one and you'll see that you are doing aggregates on all except one column.
I am trying to make a query, which returns tipo_id from a table, depending on the value of this I want to join with another table, for example if tipo_id is 1 I want to join with table called p_read if tipo_id i want to join tv_read
this is what I tried to do.
SELECT ec.id,ec.estado,fv.id,fv.num_factura,fv.importe,fv.iva,fv.total,fv.fecha_consumo_inicio,fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id, lp.id_consumo FROM aldroges8.factura_venta fv
INNER JOIN aldroges8.lectura l ON fv.id=l.facturaVenta_id
INNER JOIN aldroges8.factura_cobro fc ON fc.facturaventa_id = fv.id
INNER JOIN aldroges8.cobros c ON c.id=fc.cobros_id
INNER JOIN aldroges8.estado_cobros ec ON ec.id = c.estado
IF (l.tipo_id=1)
INNER JOIN aldroges8.lectura_potencia lp ON l.id=lp.id
ELSE IF (l.tipo_id =3)
INNER JOIN aldroges8.lectura_tv_gas lp ON lp.id=l.id
WHERE fv.factura_enviada=1 AND fv.suministro_id=:id_contrato ORDER BY fv.fecha_factura DESC;
But i am getting this error.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF (l.tipo_id==1)
INNER JOIN aldroges8.lectura_potencia lp ON l.id=lp.id
ELSE' at line 7
So I want to know if there is a way on doing this if staments on a query, or do I need to make another query with tipo_id, thanks in advance
SELECT ec.id,ec.estado,fv.id,fv.num_factura,fv.importe,fv.iva,fv.total,fv.fecha_consumo_inicio,fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id,
/* Used case when statement to get the required result in that column */
case when l.tipo_id=1 then lp_1.id_consumo
when l.tipo_id=3 then lp_3.id_consumo end as id_consumo
FROM aldroges8.factura_venta fv
INNER JOIN aldroges8.lectura l ON fv.id=l.facturaVenta_id
INNER JOIN aldroges8.factura_cobro fc ON fc.facturaventa_id = fv.id
INNER JOIN aldroges8.cobros c ON c.id=fc.cobros_id
INNER JOIN aldroges8.estado_cobros ec ON ec.id = c.estado
left join aldroges8.lectura_potencia lp_1 ON l.id=lp_1.id
left join aldroges8.lectura_tv_gas lp_3 ON lp_3.id=l.id
WHERE fv.factura_enviada=1 AND fv.suministro_id=:id_contrato ORDER BY fv.fecha_factura DESC;
I would write this with the condition in the on clause and then use coalesce() in the select:
SELECT ec.id, ec.estado, fv.id, fv.num_factura, fv.importe, fv.iva,
fv.total, fv.fecha_consumo_inicio, fv.fecha_consumo_fin,
fv.fecha_factura, fv.fichero, c.total, l.tipo_id,
coalesce(lp_1.id_conumo, lp_3.id_consumo) as id_consumo
FROM aldroges8.factura_venta fv INNER JOIN
aldroges8.lectura l
ON fv.id = l.facturaVenta_id INNER JOIN
aldroges8.factura_cobro fc
ON fc.facturaventa_id = fv.id INNER JOIN
aldroges8.cobros c
ON c.id = fc.cobros_id INNER JOIN
aldroges8.estado_cobros ec
ON ec.id = c.estado LEFT JOIN
aldroges8.lectura_potencia lp_1
ON l.id = lp_1.id AND l.tipo_id = 1 LEFT JOIN
aldroges8.lectura_tv_gas lp_3
ON lp_3.id = l.id AND l.tipo_id = 3
WHERE fv.factura_enviada = 1 AND
fv.suministro_id = :id_contrato
ORDER BY fv.fecha_factura DESC;
The difference between doing the comparison in the ON verses in a CASE expression may seem subtle, but it can be important.
If there are multiple matches in either table, then putting the condition in the SELECT will result in duplicate rows.
I intend to do an update based on join, but getting an error. What is missing?
update
vna.patients,
vna.patient_observations,
vna.studies,
vna.series,
vna.instances,
vna.sop_classes,
vna.files,
vna.modalities,
vna.issuers
join patient_observations on atients.patient_id=patient_observations.patient_id
join studies
on patient_observations.study_id=studies.study_id
AND studies.patient_id=patients.patient_id
join series
on series.study_id=studies.study_id
join instances
on instances.series_id=series.series_id
join sop_classes
on sop_classes.sop_class_id=instances.sop_class_id
join files
on files.instance_id=instances.instance_id
left join modalities
on modalities.modality_id=series.modality_id
left join issuers
on (patients.issuer_of_patient_identifier=issuers.issuer_id)
set PATIENT_NAME='AAPM'
WHERE PATIENT_IDENTIFIER='TG18-2002';
ERROR 1066 (42000): Not unique table/alias: 'patient_observations'
You don't need to specify table names again which appears in join part
UPDATE
vna.patients
JOIN patient_observations
ON patients.patient_id = patient_observations.patient_id
JOIN studies
ON patient_observations.study_id = studies.study_id
AND studies.patient_id = patients.patient_id
JOIN series
ON series.study_id = studies.study_id
JOIN instances
ON instances.series_id = series.series_id
JOIN sop_classes
ON sop_classes.sop_class_id = instances.sop_class_id
JOIN files
ON files.instance_id = instances.instance_id
LEFT JOIN modalities
ON modalities.modality_id = series.modality_id
LEFT JOIN issuers
ON (patients.issuer_of_patient_identifier = issuers.issuer_id)
SET PATIENT_NAME = 'AAPM'
WHERE PATIENT_IDENTIFIER = 'TG18-2002' ;
Why are you mixing the two different JOIN syntaxes? Simple rule: Never use commas in the FROM clause (and that goes for UPDATE as well). I think you intend:
update vna.patients p
patient_observations po
on p.patient_id = po.patient_id join
studies st
on po.study_id = st.study_id AND
st.patient_id = p.patient_id join
series s
on s.study_id = st.study_id join
instances i
on i.series_id = s.series_id join
sop_classes sc
on sc.sop_class_id = i.sop_class_id join
files f
on f.instance_id = i.instance_id left join
modalities m
on m.modality_id = s.modality_id left join
issuers iss
on (p.issuer_of_patient_identifier = iss.issuer_id)
set p.PATIENT_NAME = 'AAPM'
where p.PATIENT_IDENTIFIER = 'TG18-2002';
That seems way too complicated. I'm guessing you just want:
update vna.patients p
set p.PATIENT_NAME = 'AAPM'
where p.PATIENT_IDENTIFIER = 'TG18-2002';
The error message is very clear, the table patient_observations is listed twice in the table references in your query. Give the second one a different alias if you really need to join it again in the same query:
...
vna.issuers
join patient_observations as po2 on patients. ....
Otherwise, remove one of them.
Also try to use the ANSI SQL join syntax instead of this old syntax.
I am attempting to combine the contents of two separate (but very closely related) sql queries, but am finding it difficult to get something that doesn't return syntax errors.
The two queries that are currently working are
SELECT module.ModuleCode,
demonstrator.FK_Course,
slot.fk_ModuleCode,
programme.C_val,
Count(slotdemo.FK_Demonstrator) AS CountOfFK_Demonstrator
FROM programme
INNER JOIN (demonstrator
INNER JOIN (((module
INNER JOIN slot ON module.ModuleCode = slot.fk_ModuleCode))
INNER JOIN slotdemo ON slot.SlotNo = slotdemo.FK_SlotNo) ON demonstrator.StudentID = slotdemo.FK_Demonstrator) ON programme.COURSE = demonstrator.FK_Course
WHERE demonstrator.`undergraduate`=1
GROUP BY module.ModuleCode
and
SELECT week.Hour,
module.color,
module.moduleName,
module.num_ugdemos,
slot.fk_Room,
slot.fk_ModuleCode,
slottime.FK_Hour,
slottime.FK_SlotNo,
programme.C_val,
Count(slotdemo.FK_Demonstrator) AS CountOfFK_Demonstrator
FROM week
INNER JOIN ((programme
INNER JOIN ((module
INNER JOIN slot ON module.ModuleCode = slot.fk_ModuleCode)
INNER JOIN slottime ON slot.SlotNo = slottime.FK_SlotNo) ON programme.COURSE = module.FK_Course)
LEFT JOIN slotdemo ON slot.SlotNo = slotdemo.FK_SlotNo) ON week.Hour = slottime.FK_Hour
GROUP BY week.Hour,
module.ModuleCode,
module.moduleName,
module.color,
slot.SlotNo,
slot.fk_Room,
slottime.FK_Hour,
programme.C_val HAVING((programme.C_val)<9);
my preliminary attempt at combining the two is
SELECT
week.Hour,
module.color,
module.moduleName,
module.num_ugdemos,
slot.fk_Room,
slot.fk_ModuleCode,
slottime.FK_Hour,
slottime.FK_SlotNo,
programme.C_val,
Count(slotdemo.FK_Demonstrator) AS CountOfFK_Demonstrator
FROM
week
INNER JOIN
(
(
programme
INNER JOIN
(
demonstrator
INNER JOIN
(
(
(
module
INNER JOIN
slot
ON module.ModuleCode = slot.fk_ModuleCode
)
)
INNER JOIN
slotdemo
ON slot.SlotNo = slotdemo.FK_SlotNo
)
ON demonstrator.StudentID = slotdemo.FK_Demonstrator
)
ON programme.COURSE = demonstrator.FK_Course
)
ON week.Hour = slottime.FK_Hour
WHERE
demonstrator.`undergraduate`=1
GROUP BY week.Hour,
module.moduleName,
module.color,
slot.SlotNo,
slot.fk_ModuleCode,
slot.fk_Room,
slottime.FK_Hour,
programme.C_val
The error message, for what's it's worth (which is not much) is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'ON week.Hour = slottime.FK_Hour WHERE
demonstrator.undergraduate=1 GROUP BY we' at line 17
I think you want to UNION Two datasets together. Just use the UNION operator:
(SELECT .... FROM t1)
UNION
(SELECT ... FROM t2)
Both selects should return the same columns, IF the names differ you can use AS to rename it.
I have the SQL to display ALL the activities and relative Admin permissions (if any) for that activity.
Current SQL Code:
SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
WHERE `activities`.`active` = 1
Returning:
id | name | description | active | admin_role_id (or null)
I then need to detect whether they are an active member within that Activity.
I have the following SQL code:
SELECT DISTINCT `products`.`activity_ID` as joinedID
FROM (`transactions_items`)
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`
JOIN `activities` ON `activities`.`id` = `products`.`activity_ID`
WHERE `transactions`.`member_id` = 27500
AND `activities`.`active` = 1
Is there any way to merge this into one SQL query. I can't figure out how to use the correct JOIN queries, because of the complexity of the JOINs.
Help please, thanks! :)
Try like this
SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
JOIN (`transactions_items`
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`)
ON `activities`.`id`=`products`.`activity_ID`
WHERE `transactions`.`member_id` = 27500
AND `activities`.`active` = 1
Seems to me that a query like this would be marginally more comprehensible and (I think) adhere more closely to the spec...
SELECT c.*
, d.admin_role_id
FROM activities c
LEFT
JOIN admins d
ON d.activity_id = c.id
AND d.member_id = 27500
LEFT
JOIN products p
ON p.activity_ID = c.id
LEFT
JOIN transactions_items ti
ON ti.product_id = p.id
LEFT
JOIN transactions t
ON t.id = ti.id
AND t.member_id = 27500
WHERE c.active = 1