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";
Related
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'
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.
For some reason the member id field(auto inc.) in my huge query is returning null.I've tried every which way of selecting it... m.member_id AS member_id, etc.I cannot figure out why it is returning null when there is a value for that field in the table.
<?php
public function get_info($criteria = 0){
if(is_numeric($criteria)){
$where = "WHERE m.member_id = ".$criteria;
} else {
$where = "WHERE email_address = '".$criteria."'";
}
$query_member = "
SELECT
m.member_id AS member_id, m.display_name, m.email_address, m.group_id, m.status, m.activation_code, UNIX_TIMESTAMP(m.date_joined) AS date_joined,
m.gender, m.location, m.biography, m.mantra, m.birth_date, m.results_per_page, m.admin_emails, m.member_emails, m.last_active, m.avatar_id,
m.banner_id, m.signature, m.newsletter_subscription, m.recruiting_status, m.facebook_username, m.website, m.steam_username, m.xboxlive_gamertag, m.psn_id,
g.group_id, g.title, g.description,
a.attachment_id, a.file_name,
f.message_id, f.author_id, COUNT(f.message_id) AS forum_count,
b.attachment_id AS banner_id, b.file_name AS banner_file,
mr.request_id, mr.author_id, mr.recipient_id, mr.status, COUNT(mr.request_id) AS total_friends,
tm.team_member_id, tm.member_id, tm.team_id
FROM members AS m
LEFT JOIN member_groups AS g ON (m.group_id = g.group_id)
LEFT JOIN attachments AS a ON (m.avatar_id = a.attachment_id)
LEFT JOIN forum_messages AS f ON (m.member_id = f.author_id)
LEFT JOIN attachments AS b ON (m.banner_id = b.attachment_id)
LEFT JOIN member_requests AS mr ON (m.member_id = mr.author_id OR m.member_id = mr.recipient_id) AND mr.status = 1
LEFT JOIN team_members AS tm ON (m.member_id = tm.member_id) AND date_left = ''
".$where."
GROUP BY m.member_id
LIMIT 1";
//show_error($query_member);
if($query_member = $this->db->query($query_member)){
if($query_member->num_rows() > 0){
var_dump($query_member->row_array());
Because you select two fields with the same name. So MySQL will return result of last one. Add aliases:
SELECT m.member_id AS member_id_1, tm.member_id AS member_id_2 ...
Here is the code I'm currently using:
$result = mysql_query("
SELECT SUM(s.amount)
FROM tblaffiliatespending s
JOIN tblaffiliatesaccounts a
ON a.id=s.affaccid
JOIN tblhosting h
ON h.id = a.relid
JOIN tblproducts p
ON p.id = h.packageid
JOIN tblclients c
ON c.id = h.userid
WHERE affiliateid = $affiliateid
ORDER
BY clearingdate DESC;
");
$data = mysql_fetch_array($result);
$pendingcommissions = $data['?????????'];
$this->assign("pendingamount", $pendingcommissions);
What I'm not sure about is what to enter for ????????? on the third line. I've tried all of these things and none of them have worked:
$pendingcommissions = $data['SUM(tblaffiliatespending.amount)'];
$pendingcommissions = $data['SUM'];
$pendingcommissions = $data['tblaffiliatespending.amount'];
$pendingcommissions = $data['tblaffiliatespending'];
$pendingcommissions = $data['amount'];
Any ideas on what this needs to be changed to?
You need to give the alias for the sum of amount SUM(s.amount) total_amountso when you execute query and fetch the results from it you will have the sum of your amount column on total_amount index in resultant array and you can access it by $data['total_amount'];, also note using aggregate functions without grouping then will result in a single row not per group
SELECT SUM(s.amount) total_amount
FROM tblaffiliatespending s
JOIN tblaffiliatesaccounts a
ON a.id=s.affaccid
JOIN tblhosting h
ON h.id = a.relid
JOIN tblproducts p
ON p.id = h.packageid
JOIN tblclients c
ON c.id = h.userid
WHERE affiliateid = $affiliateid
ORDER
BY clearingdate DESC
function search_num_rows($param){
$company_name=$param['company_name'];
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' if($company_name){ AND company_name= '$company_name'} ")->result();
return $q[0]->num_rows;
}
can i insert the php code as i done in where clause.Is there any other way to do this without using active records.
It's actually very easy:
function search_num_rows($param){
$company_name = (isset($param['company_name']) && !empty($param['company_name']) ? " AND company_name = '$param[company_name]'" : '');
$loan_no=$param['loan_no'];
$q = $this->db->query("select Count(0) as num_rows
from contact_new
inner join companies c on contact_new.company_id = c.id
inner join history on contact_new.id = history.receiver_email
inner join escalation_level on contact_new.escalation_level_id = escalation_level.id
inner join departments on contact_new.departmend_id = departments.id
WHERE loan_no= '$loan_no' $company_name")->result();
return $q[0]->num_rows;
}