簡體   English   中英

如果mysql和php中的第一個查詢為空,如何執行第二個查詢?

[英]How to perform a second query if first query is empty in mysql and php?

我有一個 html 表單,它將值傳遞給一個執行查詢並顯示結果的 php 文件。
現在我希望如果在第一個查詢中結果為空(0 行),則執行完全相同的查詢,但在另一個表上執行並顯示結果。

這是執行第一個查詢的代碼:

<?php 

echo "<table style='border: solid 1px black;'>";
echo "<tr>
<th>R1</th>
<th>R2</th>
<th>R3</th>
<th>R4</th>
<th>R5</th>
</tr>";


class TableRows1 extends RecursiveIteratorIterator {
function __construct($it1) {
    parent::__construct($it1, self::LEAVES_ONLY);
}

function current() {
    return "<td style='width: 70px;'>" . parent::current(). "</td>";
}

function beginChildren() {
    echo "<tr>";
}

function endChildren() {
    echo "</tr>" . "\n";
}
}

if( isset($_POST['submit']) )
{
    $feature = $_POST['R1'];
    $feature2 = $_POST['R2'];
    $feature3 = $_POST['R3'];
    $feature4 = $_POST['R4'];
    $feature5 = $_POST['R5'];
};

$feature = $_POST['R1'];
$feature2 = $_POST['R2'];
$feature3 = $_POST['R3'];
$feature4 = $_POST['R4'];
$feature5 = $_POST['R5'];

$values = [$feature, $feature2, $feature3, $feature4, $feature5];


$servername = "";
$username = "";
$password = "";
$dbname = "";

try {


$conn1 = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt1 = $conn1->prepare(

"SELECT
R1, 
R2, 
R3, 
R4, 
R5, 
FROM table
WHERE
R1 = ?
AND
R2 = ?
AND
R3 = ?
AND
R4 = ?
AND
R5 = ?");

$stmt1->bindParam(1, $feature, PDO::PARAM_INT);
$stmt1->bindParam(2, $feature2, PDO::PARAM_INT);
$stmt1->bindParam(3, $feature3, PDO::PARAM_INT);
$stmt1->bindParam(4, $feature4, PDO::PARAM_INT);
$stmt1->bindParam(5, $feature5, PDO::PARAM_INT);

$stmt1->execute();


// set the resulting array to associative
$result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);

foreach(new TableRows1(new RecursiveArrayIterator($stmt1->fetchAll())) as $k1=>$v1) {
    echo $v1;
}
}
catch(PDOException $e1) {
echo "Error: " . $e1->getMessage();
}
$conn1 = null;
echo "</table>";
?>

老實說,我不知道在哪里以及如何放置第二個查詢,任何想法和指導我都會非常感激!

舉例來說; 我並不是說它很漂亮,但我希望這比我的編碼技能更像是數據模型的弱點......

DROP TABLE IF EXISTS table_a;
DROP TABLE IF EXISTS table_b;

CREATE TABLE table_a(id SERIAL PRIMARY KEY);
CREATE TABLE table_b(id SERIAL PRIMARY KEY);

INSERT INTO table_b VALUES (NULL),(NULL),(NULL);

SELECT x.* 
  FROM
     ( SELECT 1 source, id FROM table_a
        UNION
       SELECT 2, id FROM table_b
     ) x
  JOIN
     ( SELECT MIN(source) min_source
         FROM 
            ( SELECT 1 source, id FROM table_a
               UNION
              SELECT 2, id FROM table_b
            ) n
     ) y
    ON y.min_source = x.source;
    
+--------+----+
| source | id |
+--------+----+
|      2 |  1 |
|      2 |  2 |
|      2 |  3 |
+--------+----+

根據@Strawberry 的建議,我設法通過以下查詢解決了這個問題:

SELECT 
NULL AS field1,
table1.R1,
table1.R2,
table1.R3,
table1.R4,
table1.R5,
FROM 
table1
WHERE
table1.R1 = ?
AND
table1.R2 = ?
AND
table1.R3 = ?
AND
table1.R4 = ?
AND
table1.R5 = ? 
UNION ALL
SELECT
table2.field1, 
table2.R1,
table2.R2,
table2.R3,
table2.R4,
table2.R5
FROM table2
WHERE 
table2.R1 = ?
AND
table2.R2 = ?
AND
table2.R3 = ?
AND
table2.R4 = ?
AND
table2.R5 = ?;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM