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
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
I'm combining 3-6 tables using left join. Here's some example of the code on from clause
FROM ip_ucp_01
LEFT JOIN ip_lt_01
ON ip_ucp_01.time = ip_lt_01.time
AND ip_ucp_01.date = ip_lt_01.date
LEFT JOIN ip_le
ON ip_lt_01.time = ip_le.time
AND ip_lt_01.date = ip_le.date
LEFT JOIN ip_lmiv_01
ON ip_le.time = ip_lmiv_01.time
AND ip_le.date = ip_lmiv_01.date
LEFT JOIN ip_cwg
ON ip_lmiv_01.time = ip_cwg.time
AND ip_lmiv_01.date = ip_cwg.date
LEFT JOIN ip_mtu_01
ON ip_cwg.time = ip_mtu_01.time
AND ip_cwg.date = ip_mtu_01.date
if there is no row on second table and third table, the data forth table and so on won't be displayed. I need to check whether the row on second table is exist or not. If so, it will use left join second table, if not it will use left join on third table and so on.
How about if you just JOIN with your first table? Like this:
FROM ip_ucp_01
LEFT JOIN ip_lt_01
ON ip_ucp_01.time = ip_lt_01.time
AND ip_ucp_01.date = ip_lt_01.date
LEFT JOIN ip_le
ON ip_ucp_01.time = ip_le.time
AND ip_ucp_01.date = ip_le.date
LEFT JOIN ip_lmiv_01
ON ip_ucp_01.time = ip_lmiv_01.time
AND ip_ucp_01.date = ip_lmiv_01.date
LEFT JOIN ip_cwg
ON ip_ucp_01.time = ip_cwg.time
AND ip_ucp_01.date = ip_cwg.date
LEFT JOIN ip_mtu_01
ON ip_ucp_01.time = ip_mtu_01.time
AND ip_ucp_01.date = ip_mtu_01.date
since you only join on date and time won't this do what you want?
I don't think LEFT JOIN will work the way your are asking for ,because if there is no entry in parent table your won't be able to fetch data from grandchild .
In my opinion you can simple use CASE with Count (not best practice) but it will solve your problem.
You can use separate SELECT queries for those JOINs and UNION them all (with DISTINCT) into one query, e.g.:
SELECT DISTINCT column_name
FROM (
SELECT column_name
FROM ip_ucp_01
LEFT JOIN ip_lt_01 ON ip_ucp_01.time = ip_lt_01.time AND ip_ucp_01.date = ip_lt_01.date
LEFT JOIN ip_le ON ip_lt_01.time = ip_le.time AND ip_lt_01.date = ip_le.date;
UNION
SELECT column_name
FROM ip_ucp_01
LEFT JOIN ip_lmiv_01 ON ip_ucp_01.time = ip_lmiv_01.time AND ip_ucp_01.date = ip_lmiv_01.date
) a;
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
i have 4 tables...the parent table is d_checkupinfo and i want to join it with (checkup) or (surgery) or (medicaleq) based on checkupinfo.move_type column value...if it value = 1 then join with checkup table and if value =2 then join with surgery and if value =3 then join with medicaleq.
so how to do this
SELECT
d_checkupinfo.*,
d_branch.*,
d_checkup.* ,
d_surgery.* ,
d_medicaleq.* ,
d_patient.*
FROM d_checkupinfo
LEFT JOIN d_branch WHERE d_checkupinfo.chi_branch_id = d_branch.branch_id
LEFT JOIN d_patient WHERE d_checkupinfo.chi_pi_num = d_patient.pi_id
LEFT JOIN d_checkup WHERE d_checkupinfo.chi_type_id = d_checkup.checkup_id AND move_type = 1
LEFT JOIN d_surgery WHERE d_checkupinfo.chi_type_id = d_surgery.surgery_id AND move_type = 2
LEFT JOIN d_medicaleq WHERE d_checkupinfo.chi_type_id = d_medicaleq.medicaleq_id AND move_type = 3
SELECT
d_checkupinfo.*,
d_branch.*,
d_checkup.* ,
d_patient.*
FROM d_checkupinfo
LEFT JOIN d_branch ON d_checkupinfo.chi_branch_id = d_branch.branch_id
LEFT JOIN d_patient ON d_checkupinfo.chi_pi_num = d_patient.pi_id
LEFT JOIN d_checkup ON d_checkupinfo.chi_type_id = d_checkup.checkup_id WHERE move_type = 1
UNION ALL
SELECT
d_checkupinfo.*,
d_branch.*,
d_surgery.* ,
d_patient.*
FROM d_checkupinfo
LEFT JOIN d_branch ON d_checkupinfo.chi_branch_id = d_branch.branch_id
LEFT JOIN d_patient ON d_checkupinfo.chi_pi_num = d_patient.pi_id
LEFT JOIN d_surgery on d_checkupinfo.chi_type_id = d_surgery.surgery_id WHERE move_type = 2
UNION ALL
SELECT
d_checkupinfo.*,
d_branch.*,
d_medicaleq.* ,
d_patient.*
FROM d_checkupinfo
LEFT JOIN d_branch ON d_checkupinfo.chi_branch_id = d_branch.branch_id
LEFT JOIN d_patient ON d_checkupinfo.chi_pi_num = d_patient.pi_id
LEFT JOIN d_medicaleq on d_checkupinfo.chi_type_id = d_medicaleq.medicaleq_id WHERE move_type = 3
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.