2015-11-04 64 views
0

我對數據庫運行查詢,並且必須檢查多個條件爲true。我所提供的代碼提取了我想要的數據,但是如果我必須檢查更多的列,它可能會很快失控。我不想要一個查詢,在那裏我必須寫出十幾個「AND」條件。我正在學習一些MySQL,希望得到任何幫助。尋找一個更清潔的方式來運行查詢,必須檢查多個「AND」條件

<?php 
$servername = "localhost"; 
$username = "xxxx"; 
$password = "xxxx"; 
$dbname = "oldga740_SeniorProject"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 

//Set up and run my Query 

$sql = "SELECT Project, Client, DateReceived, LastName, FinalReviewDate FROM Projects 
      WHERE FinalReviewDate IS NOT NULL 
      AND DateDelivered IS NULL 
      AND DateAccepted IS NULL 
      ORDER BY DateAccepted DESC"; 
$result = $conn->query($sql); 

//Display results 

if ($result->num_rows > 0) { 
echo "<table><tr><th>Client</th><th>Project</th><th>Point of  Contact</th><th>Date Project Received</th><th>Final Review Date</th></tr>"; 
// output data of each row 
while($row = $result->fetch_assoc()) { 
    echo "<tr><td>" . $row["Client"]. "</td><td>" . $row ["Project"]. "</td><td> " . $row["LastName"]. "</td><td> " . $row ["DateReceived"]. "</td><td> " . $row["FinalReviewDate"]. "</td></tr>"; 
} 
echo "</table>"; 
} else { 
echo "0 results"; 
} 

$conn->close(); 
?> 

回答

1

您可以爲您和條件使用數組。但是一般來說,如果你準備好陳述和綁定價值,你必須小心。此外,您還可以在類似下面的類似的方式動態地創建WHERE條款:

編輯:

$where = array(
    array(
     'column'=>'FinalReviewDate', 
     'operator'=>'AND', 
     'condition'=>'IS NOT NULL', 
     'value'=>'', 
    ), 
    array(
     'column'=>'DateDelivered', 
     'operator'=>'AND', 
     'condition'=>'IS NULL', 
     'value'=>'', 
    ), 
    array(
     'column'=>'DateAccepted', 
     'operator'=>'AND', 
     'condition'=>'IS NULL', 
     'value'=>'', 
    ), 
    array(
     'column'=>'other_column', 
     'operator'=>'OR', 
     'condition'=>'>=', 
     'value'=>5, 
    ), 
); 

$temp = array(); 

foreach($where as $key => $value) { 
if ($value['value'] == '') { 
     $temp[] = $value['operator'].' '.$value['column'].' '.$value['condition']; 
    } else { 
     $temp[] = $value['operator'].' '.$value['column'].' '.$value['condition'].' '.$value['value']; 
    } 
} 

$where_sql = '1 = 1 '.implode(' ', $temp); 

echo $where_sql; 

結果:

1 = 1 AND FinalReviewDate IS NOT NULL AND DateDelivered IS NULL AND DateAccepted IS NULL OR other_column >= 5 

OLD ANSWER

$ands = array(
    array(
     'column'=>'FinalReviewDate', 
     'condition'=>'IS NOT NULL' 
    ), 
    array(
     'column'=>'DateDelivered', 
     'condition'=>'IS NULL' 
    ), 
    array(
     'column'=>'DateAccepted', 
     'condition'=>'IS NULL' 
    ), 
); 

$temp = array(); 

foreach($ands as $key => $value) { 
    $temp[] = $value['column'].' '.$value['condition']; 
} 

$and_sql = implode(' AND ', $temp); 

echo $and_sql; 

結果:

FinalReviewDate IS NOT NULL AND DateDelivered IS NULL AND DateAccepted IS NULL 
+0

在代碼中我會放置數組?我假設它會替換WHERE和兩個AND語句... – Tony

+0

您可以將數組放在$ sql變量上方。它取代了以下內容:FinalReviewDate IS NOT NULL AND DateDelivered IS NULL AND DateAccepted IS NULL –

+0

我編輯了答案,查看最終代碼。 –

相關問題