您可以創建根據您的標準,首先動態選擇字段的查詢。例如
假設您有兩個標準傳遞給您。然後(確保您的criteria1和criteria2安全後):
$mySelect = ''; //placeholder so that you can add select fields
$extraTables = ''; //placeholder to put the extra tables I may need
$criteria = " WHERE 1 "; //this will select everything
if ($criteria1>'') {
$mySelect .= ' , t3.field3 ';
$extraTables = " , aDifferentTable AS t3";
$criteria .= " AND t3.someKey = t1.someKey '";
$criteria .= " AND field_crit1 = '" . $criteria1 . "'";
}
//and an example of connecting dynamically to an other table
if ($criteria2>'') {
$mySelect .= ' , t2.field5 ';
$extraTables = " , anOtherTable AS t2";
$criteria .= " AND t2.someKey = t1.someKey '";
$criteria .= " AND t2.field_crit2 = '" . $criteria2 . "'";
}
//lets combine all together into one dynamically created query
$myquery = "SELECT t1.something " . $mySelect . " FROM myTable AS t1";
$myquery = $myqury . $extraTables . $criteria;
您需要根據您擁有的搜索信息動態構建查詢。很難提供沒有任何細節的正確答案,或者除非您可以將其縮小到特定問題。 –
我只想問是否有比我更好的解決方案。目前沒有遇到任何其他問題 – Boog
您不會告訴我們您的解決方案是什麼,以便我們可以告訴您替代方案。 – user1092289