![](/img/trans.png)
[英]Retrieve a column from a table by comparing two different columns from different databases, using PHP MySql
[英]Retrieve sum of two different table column field in mysql using php
嘿,我被這個查詢卡住了
我有兩個具有相同列名的不同表,但具有主鍵ID的值不同
表1:q1
id ability_to_delegate communication confidence commitment
1 0 0 1 0
2 0 0 0 0
3 0 0 0 0
4 1 0 1 0
表2:q2
id ability_to_delegate communication confidence commitment
1 0 0 2 1
2 0 0 1 1
3 0 0 0 0
4 0 0 1 1
現在,我想要的是對兩個具有相同字段名但具有不同ID的不同表的值求和。
例如,我希望將表q1的id = 4即1的置信度字段值和表q2的id = 1即2的置信度字段值相加,即3。
我嘗試使用工會但沒有得到結果
$mresult=mysqli_query($con,"select sum(sm) from
(select confidence sm from q1 where id='$id'
union
select confidence sm from q2 where id='$id') ss");
while ($row1 = mysqli_fetch_assoc($mresult)){
echo "Sum ". $row1['ss'];
}
我正在警告
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in .... on line 89
請幫幫我
完成您要查找的查詢是
SELECT `q1`.`confidence` + `q2`.`confidence` AS `TotalConfidence`
FROM `q1`, `q2`
WHERE `q1`.`id` = 4
AND `q2`.`id` = 1
您可以將其插入PHP,並在適當的地方替換變量。
$mresult=mysqli_query($con,"SELECT `q1`.`confidence` + `q2`.`confidence` AS `TotalConfidence` FROM `q1`, `q2`WHERE `q1`.`id` = '{$q1id}' AND `q2`.`id` = '{$q2id}'");
while ($row1 = mysqli_fetch_assoc($mresult)){
echo "Sum ". $row1['TotalConfidence'];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.