如果設置了變量,那麼基於變量顯示結果的正確/有效方法是什麼?如果未設置變量,則不應使用AND運算符。僅當設置了變量時才使用SELECT運算符'AND'
我很抱歉,如果這是重複,我點擊建議的鏈接,他們沒有意義。
代碼的結尾附近是我的筆記,標記爲^^^^^。
例如:
$whatever = 123;
SELECT
DISTINCT terms.name as product_type_name,
tax.term_id as termidouter
FROM $wpdb->posts AS p
INNER JOIN wp_term_relationships r
ON p.ID = r.object_id
INNER JOIN wp_term_taxonomy tax
ON r.term_taxonomy_id = tax.term_taxonomy_id
INNER JOIN wp_terms terms
ON tax.term_id = terms.term_id
WHERE
tax.taxonomy = 'product_type'
AND p.post_status = 'publish'
AND p.post_type = 'product'
AND '$whatever ' = terms.term_id
^^^^ If $whatever is empty, I want to return results as if this line did not exist.
ORDER BY product_type_name
");
我打算做一個IF/ELSE但我想這是偷懶的方法。
$whatever = 123;
if (empty($whatever)) {
// SELECT without the AND
} else {
// SELECT with the AND
}
AND($ whatever = terms.term_id或$ whatever IS NULL) –