Why the output values are repeating? what should be the right query? - mysql

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.

Related

MYSQL Merge two columns from two tables and still use LEFT JOIN

So I'm having a slight problem with having to save price on a product in two different tables due to a few reasons. Is it possible to merge two columns into one? I know UNION exists but does it work with LEFT JOIN's?
Any pointers is much appreciated.
Best Regards
SELECT
si.id AS shop_item_id,
si.item_price,
s.logo_file_name,
p.cat_id AS category_id,
api.item_price AS api_price,
MAX(c.campaign_desc) AS campaignDesc,
MAX(c.campaign_type_id) AS campaignType,
MAX(c.shop_id) AS campaign_shop_id,
MAX(ct.image_name) AS campaignLogo
FROM
shop_item si
LEFT JOIN
shop s ON
s.id = si.shop_id
LEFT JOIN
product p ON
si.product_id = p.id
LEFT JOIN
campaign_category cc ON
cc.category_id = p.cat_id
LEFT JOIN
campaign c ON
c.id = cc.campaign_id AND
c.shop_id = si.shop_id AND
c.show_in_pricetable = 1 AND
NOW() BETWEEN c.date_from and c.date_to
LEFT JOIN
campaign_type ct ON
c.campaign_type_id = ct.id
LEFT JOIN
shop_api_item api ON
si.rel_feed_api = api.unique_id AND si.shop_id = api.shop_id
WHERE
si.`product_id` = 586 AND
s.`active_shop` = 1
GROUP BY
s.name,
si.id ,
si.item_price
ORDER BY
si.`item_price`,
si.`shop_id`,
c.`campaign_desc` DESC
It looks like you would benefit from the COALESCE() function.
SELECT
si.id AS shop_item_id,
COALESCE(si.item_price, api.item_price) AS coalesced_price,
...
COALESCE() takes multiple arguments, and returns the first argument that is not NULL.

One of two request make my server crash. Someone know? MYSQL

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.

Sort data in inner join query

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

mysql get X(point) along with join

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).

SQL statement 4 table connect

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'