[英]SELECT statement inner join
我想知道我是否可以对我现在拥有的三个查询中的一个进行查询。
$sql = "SELECT *
FROM joblist WHERE customer = :customer"; // this is a bindParam
$sql_val = "SELECT a.customer, SUM(a.value) AS tvalue
FROM joblist AS a
INNER JOIN joblist AS b
ON a.customer = b.customer
GROUP BY a.customer";
$sql_bal = "SELECT *
FROM (SELECT SUM(balance) AS tbalance
FROM joblist GROUP BY customer) AS total_balance
WHERE tbalance = :tbalance";
我使用的表是客户,下面是列。
ID - work_order - customer - description - value - balance - status - notes
我当然使用 php PDO 和 HTMl。
基本上,我有一个用于查询公司的搜索功能。 结果已显示,但我需要 value 和 balance 列的总计。 唯一真正有效的是从表中提取数据。 我没有成功获得价值和平衡的总数。
可能不太理解你的问题,所以我会做一些猜测:
SELECT
t1.*,
t2.tvalue,
t3.tbalance
FROM
(SELECT
*
FROM
joblist
WHERE customer = :customer) t1
JOIN
(SELECT
a.customer,
SUM(a.value) AS tvalue
FROM
joblist AS a
GROUP BY a.customer) t2
ON t1.customer = t2.customer
JOIN
(SELECT
b.customer,
SUM(b.balance) AS tbalance
FROM
joblist AS b
GROUP BY b.customer
HAVING tbalance = :tbalance) t3
ON t3.customer = t1.customer;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.