2

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

Atiq
  • 166
  • 2
  • 6
  • 21
  • 4
    You are mixing mysqli and mysql – Kamehameha Sep 26 '14 at 12:26
  • Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 26 '14 at 12:27
  • Kamehameha even after rectifing I m not getting any result – Atiq Sep 26 '14 at 12:28
  • Jay Blanchard now I m using mysqli but still not getting any result. – Atiq Sep 26 '14 at 12:29
  • It still looks like mysql_ to me. – Strawberry Sep 26 '14 at 12:30
  • Now that you've changed to `mysqli_` are you still getting the same warning? If so, it is because there is something wrong with the query - it did not return a resource. Make sure you have error checking on. – Jay Blanchard Sep 26 '14 at 12:47
  • 1
    No now I m not getting any warning Jay Blanchard – Atiq Sep 26 '14 at 12:53

1 Answers1

2

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'];
}
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Jay Blanchard its properly working in mysql but not working with php. I think there is some issue in `q1`.`id` = '$q1id' AND `q2`.`id` = '$q2id' in php – Atiq Sep 26 '14 at 12:52
  • Are you setting those two variables correctly? Check my update for one other syntax change. Are you getting any errors? – Jay Blanchard Sep 26 '14 at 12:53
  • yes, 1st I tried q1.id = '$q1id' AND q2.id = '$q2id' I m getting no output 2nd I tried q1.id = '$id' AND q2.id = '$id' I m getting no output 3rd I tried q1.id = '{$q1id}' AND q2.id = '{$q2id}' I m getting no output 4th I tried q1.id = '$q1.id' AND q2.id = '$q2.id' I m getting output as 2 – Atiq Sep 26 '14 at 13:02
  • You haven't show us how you're setting the id variables that you want to use in the query. Can you update your original post? – Jay Blanchard Sep 26 '14 at 13:04
  • 1
    Issue resolved it was my mistake I was using $a1 and $a2 for passing the value. So it is q1.id = '$a1' AND q2.id = '$a2' output 3 and that is correct. Thanks. – Atiq Sep 26 '14 at 13:11