My table structure:
Lantern
Lantern_type
Location
Loan
Borrower
My query returns duplicates, I am looking for a solution which does not include repeated data.
Code
SELECT
l.lantern_id, l.lantern_name, l.lantern_state,lt.lantern_type,
lt.lantern_type_description, lt.lantern_type_tech_info,
lt.lantern_type_lens, lo.location_id, lo.location_name, loa.loan_id,
loa.loan_start_date, loa.loan_end_date, b.borrower_id, b.borrower_name, b.user_id
FROM lantern as l
INNER JOIN lantern_type as lt
ON l.lantern_id = lt.lantern_type_id
INNER JOIN location as lo
ON lt.LANTERN_TYPE_ID = l.lantern_id
INNER JOIN loan as loa
ON lo.LOCATION_ID = loa.LOAN_ID
INNER JOIN borrower as b
ON loa.loan_id = b.borrower_id
;
I know this is an old question and probably not relevant to your case now but I cannot agree with the accepted answer. In this case the accepted answer is just masking the problem.
From looking at your join statements your issue was probably with this join:
INNER JOIN location as lo
ON lt.LANTERN_TYPE_ID = l.lantern_id
You don't use the correct table alias on this join. I suspect the alias after the = should have been lo instead of l.
Try:
select l.lantern_id, l.lantern_name, l.lantern_state,lt.lantern_type,
lt.lantern_type_description, lt.lantern_type_tech_info,
lt.lantern_type_lens, lo.location_id, lo.location_name, loa.loan_id,
loa.loan_start_date, loa.loan_end_date, b.borrower_id, b.borrower_name, b.user_id
From lantern as l
INNER JOIN lantern_type as lt ON
l.lantern_id = lt.lantern_type_id
INNER JOIN location as lo ON
lt.LANTERN_TYPE_ID = l.lantern_id
INNER JOIN loan as loa ON
lo.LOCATION_ID = loa.LOAN_ID
INNER JOIN borrower as b ON
loa.loan_id = b.borrower_id
GROUP BY l.lantern_id;
See MySql GROUP BY
Related
From these tables I have written this subquery and its giving results as per requirements.
Needs expert guidence to improve this query or if we can also be able to use join for these tables.
Query:
select ps,st from pac where con in (select
config from config where logi in
( select id from logicalnode where physi
in (select id from ysicalnode where mas =11)));
SELECT
payloadstr
,starttime
FROM packetdb.packet
INNER JOIN packetdb.configuration
ON packetdb.packet.configid = packetdb.configuration.idconfig
INNER JOIN packetdb.logicalnode
ON packetdb.configuration.idconfig = packetdb.logicalnode.id
INNER JOIN packetdb.physicalnode
ON packetdb.logicalnode.physicalnodeid = packetdb.physicalnode.id and packetdb.physicalnode.macaddress=117769729
You could try using exists:
select payloadstr,starttime from packetdb.packet p
where exists(select 1 from packetdb.configuration c
where p.configid = id
and exists(select 1 from packetdb.logicalnode l
where c.logicalnodeid = id
and exists(select 1 from packetdb.physicalnode
where macaddress = 117769729
and l.physicalnodeid = id)
Using Left join
select payloadstr,starttime
from packet
left join Configuration on Configuration.IDconfig = packet.configID
left join logicalnode on logicalnode.ID = Configuration.logicalnodeid
left join physicalnode on physicalnode.ID = logicalnode.physicalnodeid
where macaddress =117769729
You can try below - using JOIN
select payloadstr,starttime
from packetdb.packet a inner join packetdb.configuration b on a.configid=b.idconfig
inner join packetdb.logicalnode c on logicalnodeid=c.id
inner join packetdb.physicalnode d on physicalnodeid=d.id
where macaddress =117769729
Try this
select pa,sta
from
pack p
INNER JOIN
confi c
ON
p.confi = c.idco
INNER JOIN
logice l
ON
c.logic = l.id
INNER JOIN
physiode pn
ON
l.physicalnodeid = pn.id
WHERE macaddress =123
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.
select Distinct
_Ad.ad_id, _Ad.Ad_Name,
ID.Image_Path, VM.year,
VD.Vehicle_Transformation, VD.Vehicle_Fuel_Type, VD.Vehicle_Mileage
from
_Ad
order by
Ad_Date_Created
inner join
_Image_Details ID on ID.ad_id = _Ad.ad_id
inner join
_Vehicle_Model VM on VM.vehicle_model_id = _AD.vehicle_model_id
inner join
_Vehicle_Details VD on _ad.ad_id = VD.ad_id;
I keep getting an error that multi part data can not be bound. Please help to correct query
Try this:
select Distinct
_Ad.ad_id, _Ad.Ad_Name,
ID.Image_Path, VM.year,
VD.Vehicle_Transformation, VD.Vehicle_Fuel_Type, VD.Vehicle_Mileage
from
_Ad
inner join
_Image_Details ID on ID.ad_id = _Ad.ad_id
inner join
_Vehicle_Model VM on VM.vehicle_model_id = _AD.vehicle_model_id
inner join
_Vehicle_Details VD on _ad.ad_id = VD.ad_id;
order by
Ad_Date_Created
The syntax of your SQL statement is wrong. An ORDER BY clause should come after the JOIN's
What I want to do (for each flight), is to select Flight_number, Departure_airport' s Name And Arrival_airport' s Name . Departure has MIN Leg_number, Arrival has MAX Leg_number.
I have tried this. But join parts or what else missing, here is the link:http://sqlfiddle.com/#!2/263a2/5
Seems odd.. but this might be what you're after...
We get the min/max leg for each flight in subquery aliased "Z"
We use this to join back to flight_leg twice, once for departure and once for arrivals
and again join back twice to airport once for departures once for arrivals.
SELECT Z.Flight_Number, DA.Name DeptName, AA.Name ArrivName
FROM (SELECT MIN(Leg_Number) MLN, MAX(Leg_Number) MxLN, Flight_Number
FROM Flight_Leg Group by Flight_Number) Z
INNER JOIN Flight_Leg D
on D.Flight_Number = Z.Flight_Number
and D.Leg_Number = Z.MLN
INNER JOIN Flight_Leg A
on A.Flight_Number = Z.Flight_Number
and A.Leg_Number = Z.MxLN
INNER JOIN AirPort DA
on DA.AirPort_Code = D.Departure_AirPort_Code
INNER JOIN AirPort AA
on AA.AirPort_Code = A.Arrival_AirPort_Code
http://sqlfiddle.com/#!2/263a2/56
Not entirely sure if this is what you're after. It's written in MS SQL so the syntax will need some minor tweaks.
SELECT fl.Flight_number,
ao.Name,
ai.Name,
(select min(Leg_number) from FLIGHT_LEG fa where fl.Flight_number
= fa.Flight_number) as min_leg_number,
(select max(Leg_number) from FLIGHT_LEG fb where fl.Flight_number
= fb.Flight_number) as max_leg_number
FROM Flight_leg Fl
inner join AIRPORT as ao on fl.Departure_airport_code =
ao.Airport_code
inner join AIRPORT as ai on fl.Arrival_airport_code =
ai.Airport_code
I have the following SQL:
$queryString = "
SELECT
iR.lastModified,
d.*,
c2.title as stakeholderTitle,
u.username as authorUsername,
c.title as authorContactName,
GROUP_CONCAT(iR.stakeholderRef) AS participants
FROM
informationRelationships iR,
contacts c2
INNER JOIN
debriefs d ON
d.id = iR.linkId
LEFT JOIN
users u ON
u.id = iR.author
LEFT JOIN
contacts c ON
c.ref = u.contactId
LEFT JOIN
debriefs d2 ON
d2.stakeholder = c2.ref
WHERE
(
iR.clientRef = '$clientRef' OR
iR.contactRef = '$contactRef'
)
AND
iR.projectRef = '$projectRef' AND
iR.type = 'Debrief'
GROUP BY
iR.linkId
ORDER BY
d.dateOfEngagement
";
notice how I require 2 different bits of data for the the contacts table.
So at one point, I need to match
c.ref = u.contactId
This will return one bit of information
but I also need a completely different grouping:
d2.stakeholder = c2.ref
Problem is that the title is the column i'm interested in for both:
c2.title as stakeholderTitle,
...
c.title as authorContactName
How do I go about doing this?
My current try is returning:
Error: Unknown column 'iR.linkId' in 'on clause'
I'm not sure I really understand what is happening here:
how to join two tables on common attributes in mysql and php?
EDIT::::---ANSWERED--zerkms
$queryString = "
SELECT
iR.lastModified,
d.*,
c2.title as stakeholderTitle,
u.username as authorUsername,
c.title as authorContactName,
GROUP_CONCAT(iR.stakeholderRef) AS participants
FROM
informationRelationships iR
INNER JOIN
debriefs d ON
d.id = iR.linkId
INNER JOIN
contacts c2 ON
d.stakeholder = c2.ref
LEFT JOIN
users u ON
u.id = iR.author
LEFT JOIN
contacts c ON
c.ref = u.contactId
WHERE
(
iR.clientRef = '$clientRef' OR
iR.contactRef = '$contactRef'
)
AND
iR.projectRef = '$projectRef' AND
iR.type = 'Debrief'
GROUP BY
iR.linkId
ORDER BY
d.dateOfEngagement
";
By re-ordering my query I have managed to get both columns in... Thanks zerkms!
You cannot mix implicit joins and explicit joins in a single query in mysql.
So
FROM informationRelationships iR,
contacts c2
should be rewritten to
FROM informationRelationships iR
INNER JOIN contacts c2 ON ...
Do not use cartesian product and joins in the same query (not subquery), here, use only joins (CROSS JOIN is the same as cartesian product).