Sql语句 - 按NOW()和NULL

时间:2015-12-27 21:10:23

标签: mysql sql

我有一个问题。我的陈述有什么问题?

"select " . $select_column_list . " p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_status = '1' and p.products_date_available <= now() or p.products_date_available is NULL and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$current_category_id . "'";

如果我向此添加or p.products_date_available is NULL我收到错误&#34; 2013 - 查询期间与MySQL服务器的连接丢失&#34;和#34; 2006 - MySQL服务器已经消失了#34;。

使用此语句,我想从数据库中选择到目前为止可用的所有产品,或者在字段products_date_available

字段中将状态设置为NULL

2 个答案:

答案 0 :(得分:2)

不确定它与&#34;丢失的连接有关?&#34;错误,但无论如何确定(或者它不会按预期工作):在WHERE条件中添加or p.products_date_available is NULL时,必须添加括号,如...and (p.products_date_available <= now() or p.products_date_available is NULL) and...。< / p>

答案 1 :(得分:1)

我认为cFreed是正确的。您必须将括号括作AND takes precedence over ORerror通常与超时有关,我在phpmyadmin中查询非常大的数据时看到了它。

[编辑]

由于提供的格式很糟糕而且这篇文章太长而无法发表评论,我想提一下:

您可以将您的字符串放在多行+ variable parsing

以便您的查询更具可读性:

"select $select_column_list p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, 
   IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, 
   IF(s.status, s.specials_new_products_price, p.products_price) as final_price 
from TABLE_PRODUCTS_DESCRIPTION pd, TABLE_PRODUCTS p 
   left join TABLE_MANUFACTURERS m on p.manufacturers_id = m.manufacturers_id 
   left join TABLE_SPECIALS s on p.products_id = s.products_id, 
   TABLE_PRODUCTS_TO_CATEGORIES p2c 
where p.products_status = '1' and (p.products_date_available <= now() or p.products_date_available is NULL) 
   and p.products_id = p2c.products_id and pd.products_id = p2c.products_id 
   and pd.language_id = $languages_id and p2c.categories_id = $current_category_id"