我試圖做一個查詢總是返回一個結果。 使用如果第一個選擇不存在,使SQL查詢選擇另一個結果?
SELECT art_price, art_header FROM 'signs' WHERE art_number=113
,如果113不存在,我希望查詢做
SELECT art_price, art_header FROM 'signs' WHERE art_number=111
我已經盡我所能與IFNULL和COALESCE和CASE,但沒有得到它的工作。
我該怎麼辦?
好像我的PHP是搞亂什麼@Felix Pemittan解決了我
<?php
include 'dbc.php';
$query = "SELECT
art_price, art_header, art_pic, art_row1, art_row2, art_row3, art_row4
FROM `signs`
WHERE art_number = ?
UNION ALL
SELECT
art_price, art_header, art_pic, art_row1, art_row2, art_row3, art_row4
FROM `signs`
WHERE
art_number = 111
AND NOT EXISTS(
SELECT 1
FROM `signs`
WHERE art_number = ?
)";
if($stmt = $conn->prepare($query)){
$stmt->bind_param('s', $_POST['art_number']);
$stmt->execute();
$stmt->bind_result($rowPrice, $rowHeader, $rowPic, $rowArt1, $rowArt2, $rowArt3, $rowArt4);
while($stmt->fetch()){
?>
這兩個sql總是會給你一個記錄還是沒有? – Blank