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

时间:2018-01-17 17:58:44

标签: php sql

我们可以从同一数据库和相同数据类型的两个不同表中添加两个select sql查询。这两个查询在满足where条件后从表中选择唯一的单元格。现在我想总结这两个具有相同数据类型的查询(即float类型)。如何执行添加操作: 对于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?

想要从两个不同的表中添加单元格,并希望将其保存在新变量中。     请告诉我如何执行上述操作。我是sql的新手。     这是我的完整代码:

    <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>

1 个答案:

答案 0 :(得分:3)

您可以执行JOIN(在示例中暗示INNER JOIN)查询:

var mycss = window.getComputedStyle(document.getElementById("body"));
myelement.innerHTML = mycss.getPropertyValue("background-color");

根据更多信息,您应该这样做:

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