在mysql和php中,如果第一个查询为空,如何执行第二个查询?
我有一个html表单,它将值传递给执行查询并显示结果的php文件。在mysql和php中,如果第一个查询为空,如何执行第二个查询?,php,html,mysql,forms,Php,Html,Mysql,Forms,我有一个html表单,它将值传递给执行查询并显示结果的php文件。 现在我想知道,如果在第一个查询中,结果是空的(0行),则执行完全相同的查询,但要覆盖另一个表并显示结果 以下是执行第一个查询的代码: <?php echo "<table style='border: solid 1px black;'>"; echo "<tr> <th>R1</th> <th>R2</th> <
现在我想知道,如果在第一个查询中,结果是空的(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 |
+--------+----+
根据@草莓的建议,我通过以下查询解决了这个问题:
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 = ?;
为什么不在两个表上只执行一个查询,而在第一个表为空的情况下只显示第二个表的结果呢?要对多个表执行查询,据我所知,它们必须有共同的行,在这种情况下,两个表没有共同的行。情况并非如此。