【发布时间】:2017-12-30 07:10:06
【问题描述】:
如果设置了变量,那么基于变量显示结果的正确/有效方式是什么?如果未设置变量,则不应使用 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 OR $whatever IS NULL)
标签: sql variables if-statement operator-keyword