I am trying to run a select query joining multiple tables. Ont of the tables has got a column coordinate with point type. Everything works correctly but in joins it does not allow me to select X(coordinate) or X(point(coordinate)).
I can select coordinate in the join and can select X(coordinate) directly on the table but both together dont work.
select x(coordinate) from location_coordinate
The above one works
select ca.campus_id,
ca.campus_name,
ca.status_code,
ca_loc.location_id,
ca_loc.address,
ca_coo.coordinate,
ca_loc.locality_id,
ca_loc.area_id,
ca_loc.city_id,
ca_loc.state_id,
loc_locality.name as locality_name,
loc_area.name as area_name,
loc_city.name as city_name,
loc_state.name as state_name
from campus_account ca
left join location ca_loc
on ca_loc.location_id=ca.location_id
left join location_coordinate ca_coo
on ca_loc.location_id=ca_coo.location_id
left join location_master loc_locality
on(ca_loc.locality_id = loc_locality.location_master_id)
left join location_master loc_area
on(ca_loc.area_id = loc_area.location_master_id)
left join location_master loc_city
on(ca_loc.city_id = loc_city.location_master_id)
left join location_master loc_state
on(ca_loc.state_id = loc_state.location_master_id);
This also works. But if i try to do
select ca.campus_id,
ca.campus_name,
ca.status_code,
ca_loc.location_id,
ca_loc.address,
ca_coo.X(coordinate),
ca_loc.locality_id,
ca_loc.area_id,
ca_loc.city_id,
ca_loc.state_id,
loc_locality.name as locality_name,
loc_area.name as area_name,
loc_city.name as city_name,
loc_state.name as state_name
from campus_account ca
left join location ca_loc
on ca_loc.location_id=ca.location_id
left join location_coordinate ca_coo
on ca_loc.location_id=ca_coo.location_id
left join location_master loc_locality
on(ca_loc.locality_id = loc_locality.location_master_id)
left join location_master loc_area
on(ca_loc.area_id = loc_area.location_master_id)
left join location_master loc_city
on(ca_loc.city_id = loc_city.location_master_id)
left join location_master loc_state
on(ca_loc.state_id = loc_state.location_master_id);
It does not select and gives me an error saying X is not a column.
Please help
It should be X(ca_coo.coordinate) not ca_coo.X(coordinate).
Related
This is the query i wrote, but when i execute the query the values are repeating. so help me to write the right query
SELECT p.id,
p.NAME,
p.year,
p.address,
p.caste,
p.landextent,
p.adharno,
p.drillingdate,
p.pumpseterectiondate,
p.pumpsethp,
p.surveyno,
p.registrationdateinescom,
p.ymdmsdpaid,
p.ymdpaiddate,
p.energisationno,
p.energisationdate,
p.mobile,
p.remarks,
c.constituency_name constituency,
t.NAME taluka,
e.NAME escom,
d.district_name district,
division.divison_name division,
p.crsubmitted,
p.uniqueid,
p.yearofdrilling,
p.yearofpumpset,
p.yearofregistration,
p.yearofenergisation,
p.escomdivuseractive
FROM progress p
INNER JOIN constituency c
ON p.constituency_id = c.id
INNER JOIN taluka t
ON c.taluka_id = t.id
INNER JOIN district d
ON t.district_id = d.id
INNER JOIN divison di
ON d.divison_id = di.id
INNER JOIN divisons division
ON d.divisons_id = division.id
INNER JOIN escomdivison e
ON e.district_id = d.id
WHERE di.id = 3;
I think you need to remove the line
INNER JOIN divisons division on d.divisons_id=division.id
since you already have division table written on the one line above for the INNER JOIN conditions, and those(alias and table) are confused among them.
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.
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.
Hello currently i have 4 tables in my database: which are tb_student, tb_history, tb_section and tb_adviser. so far i used this line to show me the history of a given student:
$qry_display = "SELECT
a.student_id, a.section_id, a.level, a.photo, a.address, a.father_occupation, a.father_phone, a.father_company, a.mother_occupation, a.mother_phone, a.mother_company,a.gpa,
b.fname, b.sex, b.lname, b.mname, b.birth_date, b.birth_place, b.address, b.father, b.father_degree, b.mother, b.mother_degree,
c.section_name, c.adviser_id
FROM tbl_er AS a
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
WHERE a.student_id=".$id." AND a.level='1st Year'";
my main problem is now i need to show the last name of the adviser with these other information. so i was thinking of putting. Note that tb_adviser is only CONNECTED to tb_section via adviser_id
LEFT OUTER JOIN tbl_adviser AS d ON a.student_id = c.adviser_id
I added this line before there where statement. and would insert this line in my SELECT fields.
d.lname_adviser
Currently it doesn't work. Anyone would shed some light into my problem.
Seeing the db structure would help, but it looks like you are trying to have JOIN on two columns that won't match. Is a.student_id going to match c.adviser_id?
Assuming the advisor_id is id in tbl_advisor then just add
LEFT OUTER JOIN tbl_advisor AS d
ON d.id = c.advisor_id
try this:
I think you should do
LEFT OUTER JOIN tbl_adviser AS d ON d.adviser_id = c.adviser_id
So your query would be:
Select .....
FROM tbl_er AS a
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
LEFT OUTER JOIN tbl_adviser AS d ON d.adviser_id = c.adviser_id
WHERE a.student_id=".$id." AND a.level='1st Year'
Somehow am not successful with creating the query that I want.
DB is to do with locations, there are the following tables which are relevant
t_location - list of locations incl. field t_location_zipcode, and t_location_id_location
t_zipcodecity - join table just t_zipcodecity_zipcode t_zipcodecity_id_city
t_city - city list with t_city_id_city
t_citystate - join table, t_citystate_id_city, t_citystate_id_state
t_state - list of states with t_state_id_state
Initially I tried to get a list of states with locations using this query:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
ORDER BY `t_state_name_full` asc
which works fine.
Now what I also need / want which I am failing dismally at is to get the number of locations in each state. I don't know if it can be done in this one query or if i need another, either way I need help!
you can use a count and a group by. Something like this:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
, COUNT(*)
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
GROUP BY `t_state_id_state` , `t_state_name_full`
ORDER BY `t_state_name_full` asc
SELECT t_state_id_state
, t_state_name_full
, COUNT(DISTINCT t_location_id_location) AS locations_number
FROM
t_state
LEFT JOIN
t_citystate ON t_state_id_state = t_citystate_id_state
LEFT JOIN
t_city ON t_citystate_id_state = t_city_id_city
LEFT JOIN
t_zipcodecity ON t_city_id_city = t_zipcodecity_id_city
LEFT JOIN
t_location ON t_zipcodecity_zipcode = t_location_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
Ok thats looking good, my bad with the left joins, i was initially trying to get ALL the states which is why i used them. But i change that to use inner joins
SELECT t_state_id_state,
t_state_name_full,
COUNT(DISTINCT t_location_id_location) AS locations_number
FROM t_state
INNER JOIN t_citystate ON t_citystate_id_state = t_state_id_state
INNER JOIN t_city ON t_city_id_city = t_citystate_id_city
INNER JOIN t_zipcodecity ON t_zipcodecity_id_city = t_city_id_city
INNER JOIN t_location ON t_location_zipcode = t_zipcodecity_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
then i actually end up with a result that looks good !