简体   繁体   English

添加两个包含来自同一数据库的两个不同表的值的变量

[英]Adding two variables which contain the values from two different tables of same database

Can we add two select sql queries from two different tables of same database and of same data type.These two queries are selecting unique cell from the table after satisfying the where condition. 我们可以从同一数据库和相同数据类型的两个不同表中添加两个select sql查询。这两个查询在满足where条件后从表中选择唯一的单元格。 Now i want to sum up these two queries which are of same data type(ie float type). 现在我想总结这两个具有相同数据类型的查询(即浮点类型)。 How to perform addition operation: For Ex: $sql=1 and $sqlNew=2... i want $add=$sql+$sqlNew=3 如何执行加法运算:对于Ex:$ sql = 1和$ sqlNew = 2 ...我想要$ add = $ sql + $ sqlNew = 3

$sql = "SELECT num1 FROM tech WHERE name1='dsf'"; //Selecting a particular cell from table tech
$sqlNew = "SELECT num2 FROM technew WHERE name2='asd'"; //Selecting a particular cell from table technew
$add = $sql + $sqlNew; // Can this operation be perfromed?

Want to add cells from two different tables and want to save it in a new variable. 想要从两个不同的表中添加单元格,并希望将其保存在新变量中。 Please let me know how to performe above operation. 请告诉我如何执行上述操作。 I am new to sql. 我是sql的新手。 this is my complete code: 这是我的完整代码:

    <html>
<body>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sample";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$add="SELECT (tech.num1 + technew.num2) as total FROM tech, technew WHERE tech.name1 = 'def' AND technew.name2 = 'asd'"; 
$result = mysqli_query($conn, $add); 
$row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
echo $row['total']; 
$sql4 = "INSERT INTO finaladdition (id, finalAddTotal ) VALUES (NULL,'$row[total]')";
$conn->close();
?> 
</body>
</html>

You could just do a JOIN (in the example the INNER JOIN is implied) query: 你可以只做一个JOIN(在示例中暗示INNER JOIN)查询:

SELECT (a.num1 + b.num2) as total
FROM tech a, technew b
WHERE a.name1 = 'def'
AND b.name2 = 'asd'

Based on further information this is what you should do: 根据进一步的信息,这是你应该做的:

$add="SELECT (tech.num1 + technew.num2) as total FROM tech, technew WHERE tech.name1 = 'def' AND technew.name2 = 'asd'"; 
$result = mysqli_query($conn, $add); 
$row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
echo $row['total']; 

$sql4 = "INSERT INTO finaladdition (id, finalAddTotal ) VALUES (NULL,$row['total')"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM