如果设置了变量,那么基于变量显示结果的正确/有效方法是什么?如果未设置变量,则不应使用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) –