SQL Server-SELECT语句返回null

时间:2018-09-24 16:16:11

标签: mysql sql select

以下SELECT语句会得出结果

<form name="display" action="update.php" method="POST" >
    <li>Account Email:<input type="text" name="find_email" /></li>
    <li><input type="submit" name="submit" /></li>
</form>

enter image description here

下面经过编辑的SELECT语句为什么不产生任何结果?

<?php

$db     = pg_connect("host=localhost port=5432 dbname=CS2102 user=xxx password=xxx");
$email = $_POST['find_email'];
$query = "SELECT * FROM user";
$result = pg_query($db, $query) or die("Cannot execute query: $query\n");       // Query template
$row    = pg_fetch_row($result);        // To store the result row
$count = pg_query($db, "SELECT count(*) FROM user");
if($db) echo "db connected" . "num of data " . pg_num_rows($count) ." . $email;

if (isset($_POST['submit'])) {
            echo "<ul><form name='update' action='update.php' method='POST' >
            <li>New Password:</li> 
            <li><input type='text' name='new_password' value='$row[password]' /></li>
        <li><input type='submit' name='new' /></li>
        </form>
    </ul>";
}

$password = $_POST['new_password'];
$email = $_POST['find_email'];
echo $email;
if (isset($_POST['new'])) { // Submit the update SQL command
    echo $password; 
    $sql = "'UPDATE user SET password = '$password'' WHERE email = '$email'";
    echo $sql . ":)";

}

enter image description here

2 个答案:

答案 0 :(得分:6)

您添加了WHERE子句:

SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
WHERE Quantity >= 1800  -- no single row with Quantity that is higher than 1800
GROUP BY ProductID;

您可能希望在聚合后过滤值:

SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
GROUP BY ProductID
HAVING SUM(Quantity) >= 1800;   -- HAVING instead of WHERE

过滤:

  • 位置-聚合之前
  • 拥有-汇总后

答案 1 :(得分:0)

WHERE在聚合之前 ,因此(显然)它过滤掉了所有行。您需要HAVING。但是,请修复别名,以免混淆:

SELECT ProductID, SUM(Quantity) AS total_Quantity
FROM Production.ProductInventory
GROUP BY ProductID
HAVING total_Quantity >= 1800;

或者,重复表达式:

SELECT ProductID, SUM(Quantity) AS total_Quantity
FROM Production.ProductInventory
GROUP BY ProductID
HAVING SUM(Quantity) >= 1800;
相关问题