SELECT jadwal_ajar.hari, jadwal_ajar.waktu, jenjang.nama_jenjang, jurusan.nama_jurusan, kelas.nama_kelas, mapel.nama_mapel, guru.nama_guru
FROM jadwal_ajar,guru , jenjang , jurusan , kelas , mapel
LEFT JOIN jadwal_ajar as a5 ON guru.kode_guru = a5.kode_guru
LEFT JOIN jadwal_ajar as a1 ON kelas.kode_kelas = a1.kode_kelas
LEFT JOIN jadwal_ajar as a2 ON mapel.kode_mapel = a2.kode_mapel
LEFT JOIN jadwal_ajar as a3 ON j.kode_jenjang = a3.kode_jenjang
LEFT JOIN jadwal_ajar as a4 ON jurusan.kode_jurusan = a4.kode_jurusan
The error you got is because of a missing field in the table guru, you should check it out.
And, from what I can see, I think that you are trying to join tables in the from, here is the way to achieve that:
SELECT jadwal_ajar.hari, jadwal_ajar.waktu,
jenjang.nama_jenjang, jurusan.nama_jurusan, kelas.nama_kelas,
mapel.nama_mapel, guru.nama_guru
FROM jadwal_ajar
LEFT JOIN guru ON guru.kode_guru = jadwal_ajar.kode_guru
LEFT JOIN kelas ON kelas.kode_kelas = jadwal_ajar.kode_kelas
LEFT JOIN mapel ON mapel.kode_mapel = jadwal_ajar.kode_mapel
LEFT JOIN jenjang ON jenjang.kode_jenjang = jadwal_ajar.kode_jenjang
LEFT JOIN jurusan ON jurusan.kode_jurusan = jadwal_ajar.kode_jurusan
Related
I'm having problems with a MySQL Query. I have read a lot of the currently answered threads, following a lot of examples but I have not been able to solve my trouble so I come to you for help. Thanks in advance.
I have this query:
SELECT ventas.*,
clientes.nombre AS CLIENT_NOMBRE,
clientes.telefono AS TELEFONO,
users.nombre AS USERNAME,
canales_ventas.nombre AS CAN_VENTA,
estados_venta.nombre AS ESTADOS,
metodos_pago.nombre AS MET_PAGO,
canales_entrega.nombre AS CAN_ENTREGA,
canales_entrega.categoria AS CAN_ENTREGA_CAT,
COALESCE(perc.qty,0) AS DESC_PERCENT,
COALESCE(fix.qty,0) AS DESC_FIXED,
COALESCE(SUM(ventas_productos.vp_cantidad*ventas_productos.vp_precio),0) AS Total_Value
FROM ventas
JOIN clientes ON clientes.id = ventas.cliente_id
JOIN users ON users.id = ventas.vendedor_id
LEFT OUTER JOIN canales_ventas ON canales_ventas.id = ventas.canal_venta_id
LEFT OUTER JOIN estados_venta ON estados_venta.id = ventas.estado_actual
LEFT OUTER JOIN metodos_pago ON metodos_pago.id = ventas.metodo_pago
LEFT OUTER JOIN canales_entrega ON canales_entrega.id = ventas.metodo_entrega
LEFT OUTER JOIN ventas_descuentos perc ON perc.orden_id = ventas.id AND perc.tipo = 1
LEFT OUTER JOIN ventas_descuentos fix ON fix.orden_id = ventas.id AND fix.tipo = 2
LEFT JOIN ventas_productos ON ventas_productos.vp_orden_id = ventas.id GROUP BY ventas.id
But I need that all the results match ventas.estado_actual = 7
I'd appreciate a lot the help. Thanks!
EDIT1:
I have tried Using WHERE in these ways:
After the last Join:
SELECT ventas.*,
clientes.nombre AS CLIENT_NOMBRE,
clientes.telefono AS TELEFONO,
users.nombre AS USERNAME,
canales_ventas.nombre AS CAN_VENTA,
estados_venta.nombre AS ESTADOS,
metodos_pago.nombre AS MET_PAGO,
canales_entrega.nombre AS CAN_ENTREGA,
canales_entrega.categoria AS CAN_ENTREGA_CAT,
COALESCE(perc.qty,0) AS DESC_PERCENT,
COALESCE(fix.qty,0) AS DESC_FIXED,
COALESCE(SUM(ventas_productos.vp_cantidad*ventas_productos.vp_precio),0) AS Total_Value
FROM ventas
JOIN clientes ON clientes.id = ventas.cliente_id
JOIN users ON users.id = ventas.vendedor_id
LEFT OUTER JOIN canales_ventas ON canales_ventas.id = ventas.canal_venta_id
LEFT OUTER JOIN estados_venta ON estados_venta.id = ventas.estado_actual
LEFT OUTER JOIN metodos_pago ON metodos_pago.id = ventas.metodo_pago
LEFT OUTER JOIN canales_entrega ON canales_entrega.id = ventas.metodo_entrega
LEFT OUTER JOIN ventas_descuentos perc ON perc.orden_id = ventas.id AND perc.tipo = 1
LEFT OUTER JOIN ventas_descuentos fix ON fix.orden_id = ventas.id AND fix.tipo = 2
LEFT JOIN ventas_productos ON ventas_productos.vp_orden_id = ventas.id GROUP BY ventas.id
WHERE ventas.estado_actual = 7
ERROR #1064 - Something is wrong in your syntax 'LIMIT 0, 25' on line 23
In the From Clause:
SELECT ventas.*,
clientes.nombre AS CLIENT_NOMBRE,
clientes.telefono AS TELEFONO,
users.nombre AS USERNAME,
canales_ventas.nombre AS CAN_VENTA,
estados_venta.nombre AS ESTADOS,
metodos_pago.nombre AS MET_PAGO,
canales_entrega.nombre AS CAN_ENTREGA,
canales_entrega.categoria AS CAN_ENTREGA_CAT,
COALESCE(perc.qty,0) AS DESC_PERCENT,
COALESCE(fix.qty,0) AS DESC_FIXED,
COALESCE(SUM(ventas_productos.vp_cantidad*ventas_productos.vp_precio),0) AS Total_Value
FROM ventas WHERE ventas.estado_actual = 7
JOIN clientes ON clientes.id = ventas.cliente_id
JOIN users ON users.id = ventas.vendedor_id
LEFT OUTER JOIN canales_ventas ON canales_ventas.id = ventas.canal_venta_id
LEFT OUTER JOIN estados_venta ON estados_venta.id = ventas.estado_actual
LEFT OUTER JOIN metodos_pago ON metodos_pago.id = ventas.metodo_pago
LEFT OUTER JOIN canales_entrega ON canales_entrega.id = ventas.metodo_entrega
LEFT OUTER JOIN ventas_descuentos perc ON perc.orden_id = ventas.id AND perc.tipo = 1
LEFT OUTER JOIN ventas_descuentos fix ON fix.orden_id = ventas.id AND fix.tipo = 2
LEFT JOIN ventas_productos ON ventas_productos.vp_orden_id = ventas.id GROUP BY ventas.id
ERROR - #1064 Again the same error
where clause must be before group by
WHERE ventas.estado_actual = 7 GROUP BY ventas.id
Here's a query, i'm getting this error #1066 - Not unique table/alias: 'detail_penerbangan'
I have 6 Table
Table 1 penerbangan
Table 2 sdm
Table 3 pesawat
Table 4 Type Pesawat
Table 5 detail_penerbangan
Table 6 area
SELECT penerbangan.kd_penerbangan, penerbangan.kd_regispesawat, penerbangan.nrp,penerbangan.misi, penerbangan.status_penerbangan, sdm.nrp, sdm.nama_personel, pesawat.kd_regispesawat, pesawat.kd_typepesawat, type_pesawat.kd_typepesawat,type_pesawat.typepesawat,detail_penerbangan.kd_penerbangan, detail_penerbangan.kd_area_from,area.kd_area, area.singkatan_area
from penerbangan
LEFT JOIN sdm ON penerbangan.nrp = sdm.nrp
LEFT JOIN pesawat ON penerbangan.kd_regispesawat = pesawat.kd_regispesawat
LEFT JOIN type_pesawat ON pesawat.kd_typepesawat = type_pesawat.kd_typepesawat
LEFT JOIN detail_penerbangan ON penerbangan.kd_penerbangan = detail_penerbangan.kd_penerbangan
LEFT JOIN area ON detail_penerbangan.kd_area_from = area.kd_area
LEFT JOIN (SELECT penerbangan.kd_penerbangan, detail_penerbangan.kd_penerbangan, detail_penerbangan.kd_area_tujuan, area.kd_area, area.singkatan_area FROM penerbangan, detail_penerbangan
LEFT JOIN detail_penerbangan ON penerbangan.kd_penerbangan = detail_penerbangan.kd_penerbangan
LEFT JOIN area ON detail_penerbangan.kd_area_tujuan = area.kd_area
where penerbangan.kd_penerbangan = detail_penerbangan.kd_penerbangan)
AS test ON penerbangan.kd_penerbangan = test.kd_penerbangan
In second subquery you have added the table two times.. one in from with cross join and one with left join that causes this..
Use this..
SELECT penerbangan.kd_penerbangan, penerbangan.kd_regispesawat, penerbangan.nrp,penerbangan.misi, penerbangan.status_penerbangan, sdm.nrp, sdm.nama_personel, pesawat.kd_regispesawat, pesawat.kd_typepesawat, type_pesawat.kd_typepesawat,type_pesawat.typepesawat,detail_penerbangan.kd_penerbangan, detail_penerbangan.kd_area_from,area.kd_area, area.singkatan_area
from penerbangan
LEFT JOIN sdm ON penerbangan.nrp = sdm.nrp
LEFT JOIN pesawat ON penerbangan.kd_regispesawat = pesawat.kd_regispesawat
LEFT JOIN type_pesawat ON pesawat.kd_typepesawat = type_pesawat.kd_typepesawat
LEFT JOIN detail_penerbangan ON penerbangan.kd_penerbangan = detail_penerbangan.kd_penerbangan
LEFT JOIN area ON detail_penerbangan.kd_area_from = area.kd_area
LEFT JOIN (
SELECT penerbangan.kd_penerbangan, detail_penerbangan.kd_penerbangan, detail_penerbangan.kd_area_tujuan, area.kd_area, area.singkatan_area FROM penerbangan LEFT JOIN detail_penerbangan ON penerbangan.kd_penerbangan = detail_penerbangan.kd_penerbangan
LEFT JOIN area ON detail_penerbangan.kd_area_tujuan = area.kd_area
where penerbangan.kd_penerbangan = detail_penerbangan.kd_penerbangan
)
AS test ON penerbangan.kd_penerbangan = test.kd_penerbangan
Need a little help with subqueries
If I have a 1 query like this:
SELECT Learner.Learner_Id, Max(LearnerEmploymentStatus.DateEmpStatApp) AS LatestEmpDate, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON LearnerEmploymentStatus.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
WHERE EmploymentStatusMonitoring.ESMType="BSI"
GROUP BY Learner.Learner_Id, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
...and another like this:
SELECT Learner.Learner_Id, LearnerEmploymentStatus.DateEmpStatApp, EmploymentStatusMonitoring.ESMCode
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON Learner.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
...and I wanted to do a join between the 2 queries (LEFT JOIN on the common Learner_Id and LatestEmpDate / DateEmpStatApp fields), how would I go about doing all this work in a single query where the 2 queries above would be subqueries?
My attempt below is not being accepted (JOIN expression not supported):
SELECT sQ1.Learner_Id, sQ1.LearnRefNumber, sQ1.FamilyName, sQ1.GivenNames, sQ1.LatestEmpDate, sQ1.ESMType, sQ2.ESMCode
FROM
(SELECT Learner.Learner_Id, Max(LearnerEmploymentStatus.DateEmpStatApp) AS LatestEmpDate, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON LearnerEmploymentStatus.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
WHERE EmploymentStatusMonitoring.ESMType="BSI"
GROUP BY Learner.Learner_Id, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType) As sQ1
LEFT JOIN
(SELECT Learner.Learner_Id, LearnerEmploymentStatus.DateEmpStatApp, EmploymentStatusMonitoring.ESMCode
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON Learner.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id) As sQ2
ON (sQ1.Learner_Id = sQ2.Learner_Id) AND (sQ1.LatestEmpDate = sQ2.DateEmpStatApp);
Would something like this get you what you want...?
SELECT l.Learner_Id, d.LatestEmpDate, l.LearnRefNumber, l.FamilyName, l.GivenNames, m.ESMType, m.ESMCode
FROM ((Learner AS l
LEFT JOIN (
SELECT s.Learner_Id, MAX(s.DateEmpStatApp) AS LatestEmpDate
FROM LearnerEmploymentStatus AS s
GROUP BY s.Learner_Id) AS d ON d.Learner_Id = l.Learner_Id)
LEFT JOIN LearnerEmploymentStatus AS ls ON (ls.Learner_Id = d.Learner_Id) AND (ls.DateEmpStatApp = d.LatestEmpDate))
LEFT JOIN EmploymentStatusMonitoring AS m ON m.LearnerEmploymentStatus_Id = ls.LearnerEmploymentStatus_Id
WHERE m.ESMType = 'BSI'
Assumes the same learner won't have the same DateEmpStatApp twice, which may or not be valid.
The below join works in Access, but I need to add a fourth join.
FROM ((Agents
LEFT JOIN Resignation_Pool ON Agents.PF = Resignation_Pool.PF)
LEFT JOIN Teams ON Agents.Team = Teams.ID)
LEFT JOIN Skills ON Agents.PF = Skills.PF
When I add the fourth join, it doesn't work. I know Access is weird about the parenthesis, but I think I have them where they belong. The query just runs forever doing nothing (it shouldn't run long at all) and I end up cancelling it. Any suggestions?
FROM (((Agents
LEFT JOIN Resignation_Pool ON Agents.PF = Resignation_Pool.PF)
LEFT JOIN Teams ON Agents.Team = Teams.ID)
LEFT JOIN Skills ON Agents.PF = Skills.PF)
LEFT JOIN OneMore ON Agents.OM = OneMore.OM
Here is the code that works - my actual query instead of one I found that looked similar.
SELECT DISTINCT A02.PID, A02.PS, A02.PN, A02.PM, C01.RC, C01.IC, C01.RD
INTO AutoCR
FROM ((02_CorrectResults A02
LEFT OUTER JOIN dbo_pol_PGI C01
ON (A02.PID = C01.PID and C01.PS = '999' and C01.PEDate >= #04/01/2012#))
LEFT OUTER JOIN dbo_pol_IL C02
ON C01.PID = C02.PID)
LEFT OUTER JOIN dbo_pol_UN C03
ON C02.ILID = C03.ILID
I add another join and this doesn't work. I tried using inner join instead but Access doesn't like that.
SELECT DISTINCT A02.PID
, A02.PS
, A02.PN
, A02.PM
, C01.RC
, C01.IC
, C01.RD
, C04.CCode
, C04.PCode
, C04.CForm,
INTO AutoCR
FROM (((02_CorrectResults A02
LEFT OUTER JOIN dbo_rol_PGI C01
ON (A02.PID = C01.PID and C01.PS = '999' and C01.PEDate >= #04/01/2012#))
LEFT OUTER JOIN dbo_rol_IL C02
ON C01.PID = C02.PID)
LEFT OUTER JOIN dbo_rol_UN C03
ON C02.ILID = C03.ILID)
LEFT OUTER JOIN dbo_rol_HO C04
ON C03.UnID = C04.UnID
You have a trailing , in your query. Remove it.
SELECT DISTINCT A02.PID
, A02.PS
, A02.PN
, A02.PM
, C01.RC
, C01.IC
, C01.RD
, C04.CCode
, C04.PCode
, C04.CForm, <--- this was the problem
INTO AutoCR
FROM (((02_CorrectResults A02
LEFT OUTER JOIN dbo_rol_PGI C01
ON (A02.PID = C01.PID and C01.PS = '999' and C01.PEDate >= #04/01/2012#))
LEFT OUTER JOIN dbo_rol_IL C02
ON C01.PID = C02.PID)
LEFT OUTER JOIN dbo_rol_UN C03
ON C02.ILID = C03.ILID)
LEFT OUTER JOIN dbo_rol_HO C04
ON C03.UnID = C04.UnID
I have
SELECT
servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM
servisler
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
where kullanici.kullanici = 'MAR.EDI.003'
but it says
[Err] 1054 - Unknown column 'kullanici_cihaz.URUN_GRUP_NO' in 'on clause'
enen though the column exits. What is its problem?
schema
Server version: 5.1.33-community-log
I think this is because you're using the kullanici_cihaz table in your ON clause, and that before this table is linked.
Try to rearrange your INNER JOIN :
SELECT
servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM
servisler
INNER JOIN kullanici ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
WHERE
kullanici.kullanici = 'MAR.EDI.003'
EDIT
You also have some 'loop inner join', I removed it :
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
Because kullanici.SERVIS_RECNO does not exist when you join the tables: servisler, urun_gruplari. Join table kullanici first before joining urun_gruplari.
SELECT servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM servisler
INNER JOIN kullanici
ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN urun_gruplari
ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici_cihaz
ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND
kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
where kullanici.kullanici = 'MAR.EDI.003'