Retrieve sum of two different table column field in mysql using php

Atiq

Hey guys I m stuck over this query

I have two different table with same column name but different values with primary key id

Table 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

Table 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

Now what I want is to sum the values of two different tables with same field name but different IDs.

For example I want values of confidence field from table q1 with id = 4 i.e 1 and values of confidence field from table q2 with id = 1 i.e 2 to be added i.e 3.

I tried using union but not getting the rseult

$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'];
}

I m getting warning

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in .... on line 89

Please help me out

Jay Blanchard

The query to accomplish what you're looking for is

SELECT `q1`.`confidence` + `q2`.`confidence` AS `TotalConfidence`
FROM `q1`, `q2`
WHERE `q1`.`id` = 4
AND `q2`.`id` = 1

You can plug this into your PHP and substitute the variables where appropriate.

$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'];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Retrieve sum of two different table column field in mysql using php

From Dev

How to sum each column in MySQL table with PHP

From Dev

How to sum of MySQL table column in php?

From Dev

Mysql Select two column in different table

From Dev

Update query for two different Column in two different table-MySQL

From Dev

sum two or more columns in mysql table but leave out the null field

From Dev

How to insert two different check box values into different columns of one mysql table using php?

From Dev

How to find and show number of similar words in two different column within same table(MySql,PHP)

From Dev

How to Retrieve Data from Two Different MySQL Database using JDBC?

From Dev

Retrieve MySQL database column names (with table name) using Coldfusion

From Dev

connecting two different database using php/mysql

From Dev

PHP, MySQL How to Sum data in two different date range

From Dev

PHP MySQL: COUNT/SUM a non integer column with different values

From Dev

How To Use Distinct Function For Two Column And Sum on PHP MySQL?

From Dev

swap "two" column values in two different rows using mysql

From Dev

PHP - MYSQL: How to retrieve column information using Alias

From Dev

how to retrieve array values from mysql table using php?

From Dev

How to retrieve JSON data from MySQL table using PHP?

From Dev

Retrieve data from mysql database and insert it in a table using php

From Dev

Fill data of one row using other table column sum in mysql

From Dev

How to get count of two fields from two different table with grouping a field from another table in mysql

From Dev

How to get count of two fields from two different table with grouping a field from another table in mysql

From Dev

PHP & MYSQL - Query DB Table to confirm field is in a Column

From Dev

PHP & MYSQL - Query DB Table to confirm field is in a Column

From Dev

MYSQL - SUM VALUE TWO TABLE

From Dev

mysql: two table join with sum

From Dev

Using sum if with two different arguments

From Dev

How to display value of a field from a different table by using the value of a different column in another table

From Dev

MYSQL join two different columns from two different table as single column

Related Related

  1. 1

    Retrieve sum of two different table column field in mysql using php

  2. 2

    How to sum each column in MySQL table with PHP

  3. 3

    How to sum of MySQL table column in php?

  4. 4

    Mysql Select two column in different table

  5. 5

    Update query for two different Column in two different table-MySQL

  6. 6

    sum two or more columns in mysql table but leave out the null field

  7. 7

    How to insert two different check box values into different columns of one mysql table using php?

  8. 8

    How to find and show number of similar words in two different column within same table(MySql,PHP)

  9. 9

    How to Retrieve Data from Two Different MySQL Database using JDBC?

  10. 10

    Retrieve MySQL database column names (with table name) using Coldfusion

  11. 11

    connecting two different database using php/mysql

  12. 12

    PHP, MySQL How to Sum data in two different date range

  13. 13

    PHP MySQL: COUNT/SUM a non integer column with different values

  14. 14

    How To Use Distinct Function For Two Column And Sum on PHP MySQL?

  15. 15

    swap "two" column values in two different rows using mysql

  16. 16

    PHP - MYSQL: How to retrieve column information using Alias

  17. 17

    how to retrieve array values from mysql table using php?

  18. 18

    How to retrieve JSON data from MySQL table using PHP?

  19. 19

    Retrieve data from mysql database and insert it in a table using php

  20. 20

    Fill data of one row using other table column sum in mysql

  21. 21

    How to get count of two fields from two different table with grouping a field from another table in mysql

  22. 22

    How to get count of two fields from two different table with grouping a field from another table in mysql

  23. 23

    PHP & MYSQL - Query DB Table to confirm field is in a Column

  24. 24

    PHP & MYSQL - Query DB Table to confirm field is in a Column

  25. 25

    MYSQL - SUM VALUE TWO TABLE

  26. 26

    mysql: two table join with sum

  27. 27

    Using sum if with two different arguments

  28. 28

    How to display value of a field from a different table by using the value of a different column in another table

  29. 29

    MYSQL join two different columns from two different table as single column

HotTag

Archive