SQL statement 4 table connect - mysql

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'

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.

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

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.

Full outer join : Mssql to Mysql conversion

I have this query in MSSQL ..
how to convert it into MYSQL query..
CREATE VIEW DATA_VIEW AS SELECT A.*,B.FIELD_VALUE D1_VALUE,C.FIELD_VALUE D2_VALUE,D.FIELD_VALUE
C_VALUE FROM DB_DATA A
FULL OUTER JOIN T_CASE_D1 B ON A.FIELD_ID =B.FIELD_ID AND B.CASE_ID=47758
FULL OUTER JOIN T_CASE_D2 C ON A.FIELD_ID =C.FIELD_ID AND C.CASE_ID=47758
FULL OUTER JOIN T_CASE_QC D ON A.FIELD_ID =D.FIELD_ID AND D.CASE_ID=47758
WHERE A.FORM_ID=5 AND IS_ACTIVE='Y'
thanks in advance
Your where clause is turning the FULL OUTER JOIN into a LEFT JOIN anyway, so you might as well just use that:
CREATE VIEW DATA_VIEW AS
SELECT A.*, B.FIELD_VALUE D1_VALUE, C.FIELD_VALUE D2_VALUE, D.FIELD_VALUE AS C_VALUE
FROM DB_DATA A LEFT JOIN
T_CASE_D1 B
ON A.FIELD_ID = B.FIELD_ID AND B.CASE_ID = 47758 LEFT OUTER JOIN
T_CASE_D2 C
ON A.FIELD_ID = C.FIELD_ID AND C.CASE_ID = 47758 LEFT OUTER JOIN
T_CASE_QC D
ON A.FIELD_ID =D.FIELD_ID AND D.CASE_ID = 47758
WHERE A.FORM_ID=5 AND IS_ACTIVE = 'Y';
FULL OUTER JOIN is rarely needed in a database with properly maintained foreign key relationships.

Outer Join with Multiple Inner Join In a Single Query

I want to use Outer Join with inner Join in a single query
Query:
select d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry
from bkng_mst a , bkng_pax b, bkng_cust c, unit_mst d
where a.bkng_id = b.bkng_id
and b.unit_cde = d.unit_cde
and a.unit_cde = d.unit_cde
and b.cust_id = c.cust_id
and a.bkng_stat = 'CNF'
and b.bkng_pax_cancel_flg = 'N'
and a.bkng_id = 'XXXX'
Use Outer Join from Table pax_dtl pd on a.bkng_id=pd.bkng_id along with above query
UPDATED :
I think that, taking into account the information provided in your comments, the following query should be helpful:
SELECT DISTINCT
d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry,
pd.bkng_id, pd.unit_name, pd.tour_code, pd.pax_name, pd.pnr_no, pd.fare_base, pd.is_block, pd.is_system
FROM
bkng_mst a
INNER JOIN bkng_pax b
ON a.bkng_id = b.bkng_id
INNER JOIN bkng_cust c
ON b.cust_id = c.cust_id
INNER JOIN unit_mst d
ON b.unit_cde = d.unit_cde
AND a.unit_cde = d.unit_cde
LEFT OUTER JOIN pax_dtl pd
ON a.bkng_id=pd.bkng_id
WHERE
a.bkng_stat = 'CNF'
AND b.bkng_pax_cancel_flg = 'N'
AND a.bkng_id = 'XXXX'
Because of 1 to many relation between bkng_mst and pax_dtl tables, the columns d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry from above query will repeat only if for 1 particular bkng_id value there will be at least one different value among the columns pd.bkng_id, pd.unit_name, pd.tour_code, pd.pax_name, pd.pnr_no, pd.fare_base, pd.is_block.
I hope it might help you, but in case of any doubts please write.

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