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
Related
I'm trying to select an specific information from a table that depends on the conditions of two other tables. When I try separate conditions work, but when I try to work with two conditions does not.
With this I get the correct data from table:
$sql = "SELECT * FROM commerce_order WHERE m_key = 'total'";
And with this I also get the result from table:
$sql = "SELECT *
FROM commerce_order M
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete'";
but when I try this I don't get any result:
$sql = "SELECT *
FROM commerce_order M
WHERE m_key = 'total'
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete'";`
I expected the first code result to be filtered by the second code condition. I know which are the values from the first table that should return, but I don't get any.
You can't put WHERE m_key = 'total' before JOIN all your WHERE clauses should go after all your JOINs:
$sql = "SELECT *
FROM commerce_order M
JOIN commerce_order_items I
ON M.item_id = I.item_id
JOIN produtcs P
ON I.order_id = P.ID
WHERE status = 'complete' AND m_key = 'total'";
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;
}
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";
I have this query:
SELECT a.apartment_id, a.building_id, a.apartment_num, a.floor, at.app_type_desc_en AS app_type_desc,
(SELECT ROW(e.e_name, ot.otype_desc_en)
FROM TABLE_TENANTS t INNER JOIN TABLE_OWNERSHIP_TYPES ot ON ot.otype_id=t.ownership_type INNER JOIN TABLE_ENTITIES e ON
t.entity_id = e.entity_id
WHERE a.apartment_id = t.apartment_id AND t.building_id = a.building_id
ORDER BY t.ownership_type DESC LIMIT 1
) AS t_row
FROM TABLE_APARTMENTS a INNER JOIN TABLE_APPARTMENT_TYPES at ON at.app_type_id = a.apartment_type LEFT OUTER JOIN TABLE_TENANTS t ON a.building_id = t.building_id AND t.entity_id=1 AND t.status=true LEFT OUTER JOIN TABLE_COMMITTEE_DELEGATE cd ON
a.building_id = cd.building_id AND cd.entity_id=1 AND cd.status=true
WHERE a.building_id = 1 AND (t.entity_id=1 OR cd.entity_id=1)
ORDER BY a.apartment_num ASC
When I'm using PHP to read the result, I use:
while($ap = pg_fetch_array($_qry)){
}
and trying to read "e_name", which is part of the ROW(e.e_name), but I can't succeed.
Need your ideas ... please.
use pg_fetch_assoc, to retrieve data in asoc-array; But my advice, - use PDO to work with db in PHP.
I have a nested mysql_query.
$resultSub = mysql_query("SELECT *
FROM ensembles
WHERE en_name = $name
LEFT JOIN ensemble_names on ensembles.en_name = ensemble_names.en_nm_ID
LEFT JOIN students on ensembles.en_stu = students.s_ID
LEFT JOIN part_names on ensembles.en_part = part_names.p_nm_ID
ORDER BY $sort $orderBy");
The query works fine without the WHERE clause, which I thought may be filtering out rows for the LEFT JOIN command, but that's not the case.
The WHERE clause should be placed after the LEFT JOINs:
$resultSub = mysql_query("SELECT *
FROM ensembles
LEFT JOIN ensemble_names on ensembles.en_name = ensemble_names.en_nm_ID
LEFT JOIN students on ensembles.en_stu = students.s_ID
LEFT JOIN part_names on ensembles.en_part = part_names.p_nm_ID
WHERE en_name = $name
ORDER BY $sort $orderBy");
Well, you put the WHERE clause in the wrong place.
Read the documentation.