JOIN not working in php mysql - php

I am working on a project with PHP and MySQL. I am trying to run a SQL query where I need to fetch data from 5 tables. I'm getting an error on my query.
$value = json_decode(file_get_contents('php://input'));
$estId = $value->estId;
$sql = 'SELECT establishments.id, establishments.name,
establishments.stay_value, establishments.latitude,
establishments.longitude, establishments.description,
establishments.address,
facilities.id, facilities.name,
accomodations.id,accomodations.name
FROM establishments
INNER JOIN (establishments_facilities
INNER JOIN facilities
ON establishments_facilty.facility_id = facilities.id)
ON establishments.id = establishments_facility.id
INNER JOIN (establishments_accommodations
INNER JOIN accommodations
ON establishments_accommodations.accommodation_d = accomodations.id)
WHERE establishments.id ="'.$estId .'" ';
$result = $conn->query($sql);
if($result->num_rows>0){
while($row = $result->fetch_assoc()){
$json_obj = $row;
}
$json_obj['success'] = true;
echo json_encode($json_obj);
}
My 5 tables are:
establishments
1 id
2 user_id
3 name
4 logo
5 description
6 email
7 latitude
8 longitude
9 stay_value
accomodations
1 id
2 name
facilities
1 id
2 name
establishments_accommodations
1 id
2 establishment_id
3 accommodation_id
establishments_facilities
1 id
2 establishment_id
3 facility_id
Any help will be appreciated.

The query you wrote is hard to read and you should use alias for better readability and also join syntax does not look correct, here is the query with proper alias
SELECT
e.id,
e.name,
e.stay_value,
e.latitude,
e.longitude,
e.description,
e.address,
f.id,
f.name,
a.id,
a.name
FROM establishments e
INNER JOIN establishments_facilities ef on e.id = ef.id
INNER JOIN facilities f ON ef.facility_id = f.id
INNER JOIN establishments_accommodations ea on ea.establishment_id = ef.establishment_id
INNER JOIN accommodations a ON ea.accommodation_d = a.id
WHERE e.id ={some value}
Now in PHP you may have something as
$sql = "
SELECT
e.id,
e.name,
e.stay_value,
e.latitude,
e.longitude,
e.description,
e.address,
f.id,
f.name,
a.id,
a.name
FROM establishments e
INNER JOIN establishments_facilities ef on e.id = ef.id
INNER JOIN facilities f ON ef.facility_id = f.id
INNER JOIN establishments_accommodations ea on ea.establishment_id = ef.establishment_id
INNER JOIN accommodations a ON ea.accommodation_d = a.id
where e.id = '".$estId."'";
Note that where e.id = '".$estId."'" is vulnerable to sq-injection, so better to use prepared statement.

Related

mySql LEFT JOIN not returning data as expected when Condition by ID php variable

When I use the PHP $qs variable in the WHERE condition it works fine but in LEFT JOIN condition, it's not returning any data.
Problem Line: AND e.terms_id_user =
My Query: (Working)
SELECT
*,
'users_staff_driving_license' AS driving_license_link,
DATE_FORMAT(terms_date_created,'%a %d-%M-%Y %r') AS udate_created,
DATE_FORMAT(terms_date_updated,'%a %d-%M-%Y %r') AS udate_updated
FROM users AS a
LEFT JOIN users_staff_details AS b
ON a.user_id = b.users_staff_id_user
LEFT JOIN user_access AS c
ON a.user_id_access_level = c.user_access_id
LEFT JOIN businesses AS d
ON a.user_id_client = d.business_id
LEFT JOIN terms AS e
ON e.terms_id_client = 'AA0000001'
AND e.terms_id_store = '1'
AND e.terms_id_user = 1001
AND e.terms_datatype = 'users_staff_driving_license'
WHERE
a.user_id = '$qs'
My Query: (Not Working)
SELECT
*,
'users_staff_driving_license' AS driving_license_link,
DATE_FORMAT(terms_date_created,'%a %d-%M-%Y %r') AS udate_created,
DATE_FORMAT(terms_date_updated,'%a %d-%M-%Y %r') AS udate_updated
FROM users AS a
LEFT JOIN users_staff_details AS b
ON a.user_id = b.users_staff_id_user
LEFT JOIN user_access AS c
ON a.user_id_access_level = c.user_access_id
LEFT JOIN businesses AS d
ON a.user_id_client = d.business_id
LEFT JOIN terms AS e
ON e.terms_id_client = 'AA0000001'
AND e.terms_id_store = '1'
AND e.terms_id_user = '$qs'
AND e.terms_datatype = 'users_staff_driving_license'
WHERE
a.user_id = '$qs'

Need distinct value based on id when we JOIN 4 tables in PHP/MYSQL

Here is my SQL query which joins 4 tables and it works correctly.
SELECT pl.lms_id, u.id, REPLACE(trim(u.`url`), 'www.', '') AS url, trim(u.`name`) as name, p.date_removed, p.status, p.ignore_status FROM `adl_seo_status` p INNER JOIN `adl_user_profiles` u on p.profile_id = u.id INNER JOIN adl_tw_profile_acc_type ac on p.profile_id = ac.profile_id LEFT JOIN `adl_lms_prof_list` pl on u.id = pl.profile_id WHERE u.`vpg_id`='2' AND u.`status` = 'Y' and ac.acc_type_id = '2' ORDER BY u.`url` ASC, p.id DESC
I am facing an issue that, the table adl_seo_status has multiple entries for a single profile_id. So, that accounts are repeating in my listing. I want that account as a single row which means the distinct value of accounts based on profile_id.
You need to use group by, for example:
SELECT
pl.lms_id,
u.id,
REPLACE(trim(u.`url`), 'www.', '') AS url,
trim(u.`name`) AS name,
p.date_removed,
p.status,
p.ignore_status
FROM `adl_seo_status` p INNER JOIN `adl_user_profiles` u ON p.profile_id = u.id
INNER JOIN adl_tw_profile_acc_type ac ON p.profile_id = ac.profile_id
LEFT JOIN `adl_lms_prof_list` pl ON u.id = pl.profile_id
WHERE u.`vpg_id` = '2' AND u.`status` = 'Y' AND ac.acc_type_id = '2'
ORDER BY u.`url` ASC, p.id DESC GROUP BY p.profile_id;

Left join not working in my prepared statement (mysqli)

I can't get a left join to work in my prepared statement.
"SELECT DISTINCT(a.auto_id), m.merk, a.model, a.uitvoering, a.standaardtekst, a.prijs, a.prijs2, a.prijs3, a.handelsprijs, a.aanmaak, s.soort, z.prijs_id
/*,GROUP_CONCAT(DISTINCT(apc.NL) ORDER BY apc.NL ASC)*/
FROM autocom_new.auto_new a
INNER JOIN autocom_new.tbl_merken m
ON a.merk = m.merk_id
INNER JOIN autocom_new.tbl_soort s
ON a.soort = s.soort_id
INNER JOIN autocom_new.auto_zoekmachines z
ON a.auto_id = z.auto_id
/*
LEFT JOIN autocom_new.auto_accessoire acc
ON a.auto_id = acc.auto_id
LEFT JOIN autocom_new.tbl_autopricecode_new apc
ON acc.code_id = apc.code_id
*/
WHERE a.ac LIKE ? AND a.flag = ?"
The commented parts are the parts that aren't working.
I have no idea what I'm doing wrong.
EDIT
First of all I forgot that both tables have a column ac, so I've changed the where statement a bit. The left joins are working now, but the part in the select is still not working
So the problem was that I forgot a GROUP BY.
"SELECT DISTINCT(a.auto_id), m.merk, a.model, a.uitvoering, a.standaardtekst, a.prijs, a.prijs2, a.prijs3, a.handelsprijs, a.aanmaak, s.soort, z.prijs_id,
GROUP_CONCAT(DISTINCT(apc.NL) ORDER BY apc.NL ASC)
FROM autocom_new.auto_new a
INNER JOIN autocom_new.tbl_merken m
ON a.merk = m.merk_id
INNER JOIN autocom_new.tbl_soort s
ON a.soort = s.soort_id
INNER JOIN autocom_new.auto_zoekmachines z
ON a.auto_id = z.auto_id
LEFT JOIN autocom_new.auto_accessoire acc
ON a.auto_id = acc.auto_id
LEFT JOIN autocom_new.tbl_autopricecode_new apc
ON acc.code_id = apc.code_id
WHERE a.ac LIKE ? AND a.flag = ?
GROUP BY a.auto_id"

Multitable query mysql

I am in process of creating multi variable & multi table search query for Job Portal. Got some success in creating following long query which gives results as required. I need to replace above highlighted red circle delimited values (which i created using GROUP_CONCAT function in following query) with follwoing table description values.
Note:- Delimited values are not in database field. I'am creating these using following query.
My other table structure is as follows:
+---------------------------------------------+
ID Description
+---------------------------------------------+
1 Delhi
2 Mumbai
3 Chennai
4 Kolkata
+----------------------------------------------+
Please help me to guide a way forward for one, rest i'll do it myself.
Select
a.user_id, g.username, a.mobile_no, a.location, a.DOB, a.current_CTC,
a.expected_CTC, a.work_exp, a.job_type, a.designation, a.Company_name,
a.keyskills, a.profile_headline, a.resume_path, a.Notice_period,
DATEDIFF(CURDATE(),Max(b.Login_date_time)) as Last_Login_days,
Max(b.Login_date_time) as Last_Available_on_site,
(Select GROUP_CONCAT(DISTINCT c.Education_ID ORDER BY c.Education_ID
SEPARATOR ', ') user_education from user_education c where a.user_id =
c.User_ID) as Education_ID,
(Select GROUP_CONCAT(DISTINCT d.FA_ID ORDER BY d.FA_ID SEPARATOR ', ') FA
from user_fa d where a.user_id = d.User_ID) as FA_ID,
(Select GROUP_CONCAT(DISTINCT e.Industry_ID ORDER BY e.Industry_ID
SEPARATOR ', ') Industry_ID from user_industry e where a.user_id =
e.User_ID) as Industry_ID,
(Select GROUP_CONCAT(DISTINCT f.Location_ID ORDER BY f.Location_ID
SEPARATOR ', ') Location_ID from user_preferred_locations f where
a.user_id = f.User_ID) as Preferred_Location_ID
from user_details a
left join login_history b on a.user_id = b.user_id
left join users g on a.user_id = g.id
left join user_education c on a.user_id = c.User_ID
left join user_fa d on a.user_id = d.User_ID
left join user_industry e on a.user_id = e.User_ID
left join user_preferred_locations f on a.user_id = f.User_ID
INNER JOIN locations h ON FIND_IN_SET(h.location_id, f.Location_ID)
WHERE MATCH(keyskills) AGAINST('MIS, Analysis' IN BOOLEAN MODE)
And YEAR(CURDATE()) - YEAR(DOB)>= 18 And YEAR(CURDATE()) - YEAR(DOB)<= 45
And work_exp >= 2 And work_exp <= 15
And a.current_CTC>=102 And a.current_CTC<=115
And a.industry IN (10001,10002,10004)
And a.functional_area IN(1001,1002,1003)
And a.location_preferred IN (1,2,3,4,6,7)
And Education_ID IN (1,5,7,8)
And a.job_type Like '%FTP%'
And Notice_period >= 15
And b.Login_date_time>=0
And a.mobile_no is not null
And resume_path is not null
group by a.user_id

Extracting single data using limit with 5 tables involve

so i have 5 tables in which it is interconnected with foreign keys
and here is a sample output of table 3
what i wanted to do in table number 3 is to extract the SubdeptID of user with userid of 10 but in this case it has 2 userid10 so its print both. what i want to print is only the one with latter TransferID. my select statement is this
$sql_exp = "SELECT a.UserID, b.Employeename, c.TransferID, e.Department
FROM dbo.FA_Laptop a
INNER JOIN dbo.users b
on a.UserID = b.UserID
INNER JOIN dbo.SubDeptTransfer c
ON a.UserID = c.UserID
INNER JOIN dbo.SubDept d
ON c.SudDeptID = d.SubDeptID
INNER JOIN dbo.departments e
ON d.DeptID = e.DeptID
WHERE a.FAID = '$faidf' ORDER by c.TransferID DESC LIMIT 1";
my php code is
$rs = $conn->Execute($sql_exp);
if ($rs->EOF) {
echo "<tr><td>Please check for the Employee Name or the Department</td>";
} else {
while (!$rs->EOF){
echo "<tr><td>".$rs->Fields("Department")." / ".$rs->Fields("EmployeeName")."</td>";
$rs->movenext();
}
$rs->Close();
}
im having an error in "LIMIT" query.
MSSQL don't have LIMIT keyword.
Use TOP instead LIMIT.
$sql_exp = "SELECT TOP 1 a.UserID, b.Employeename, c.TransferID, e.Department
FROM dbo.FA_Laptop a
INNER JOIN dbo.users b
on a.UserID = b.UserID
INNER JOIN dbo.SubDeptTransfer c
ON a.UserID = c.UserID
INNER JOIN dbo.SubDept d
ON c.SudDeptID = d.SubDeptID
INNER JOIN dbo.departments e
ON d.DeptID = e.DeptID
WHERE a.FAID = '$faidf' ORDER by c.TransferID DESC";

Categories