2017-05-05 56 views
0

我有3個表查詢,並檢查是否有一個值使用相同的變量在三個表中的一個。此刻,我能想到的是用if else condition來檢查。下面是例子,PHP - 簡單和整潔的方式來檢查是否.. else條件

if (isset($post_val)) { 
    $var= /* mysql queries table 1 */ 
} 

if (empty($var)) { 
    $var= /* mysql queries table 2 */ 

    if (empty($var)) { 
     $var= /* mysql queries table 3 */ 
    } 
} 

是否有更短或乾淨的方式來做到這一點。

更新代碼

其實,我使用WordPress的,從是CFDB插件。我的實際代碼將會是。

if (isset($vehicle_no)) { 
    $location = do_shortcode('[cfdb-value form="season parking form_copy" filter="your-platno=' . $vehicle_no . '"'); 
} 

if (empty($location)) { 
    $location = do_shortcode('[cfdb-value form="season parking form" filter="your-platno=' . $vehicle_no . '"'); 

    if (empty($location)) { 
     $location = do_shortcode('[cfdb-value form="Season Register 2017" filter="your-platno=' . $vehicle_no . '"'); 
    } 
} 
+1

也許['UNION'](https://www.tutorialspoint.com/sql/sql-unions-clause.htm)可能對你的情況有用。 –

+0

@AlexHowansky感謝您的想法和鏈接。將通過它看。 – Amran

+0

@AlexHowansky我更新我的問題 – Amran

回答

1

您可以將表結合在一起,然後搜索那一個。即

if (isset($post_val)) { 
     $var= /* mysql queries table 1 UNION table2 UNION table3 */ ; 
} 
+0

沒有看到數據庫結構以查看是否可以在帶有連接的單個查詢中獲得三個值,UNION似乎是最佳解決方案。 – G3D

+0

@ G3D感謝您的幫助。我更新我的問題的實際代碼 – Amran

相關問題