简体   繁体   中英

Sql statement- select product by NOW() and NULL

I have a question. What is wrong in my statement?

"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 . "'";

If I add to this one the or p.products_date_available is NULL I get the error "2013 - Lost connection to MySQL server during query" and "2006 - MySQL server has gone away".

With this statement I want to select from database all products that are available till now or have the status NULL in field products_date_available

不确定是否与“丢失的连接”错误有关,但是无论如何都可以肯定(或者它无法按预期工作):在WHERE条件中添加or p.products_date_available is NULL ,您必须像这样添加括号...and (p.products_date_available <= now() or p.products_date_available is NULL) and...

I think cFreed is correct. You must put parenthesis, as AND takes precedence over OR . The error is usually related to a timeout and I have seen it when queryig very large data in phpmyadmin.

[edit]

Since formatting of the provided is awful and this is too long to fit a comment, I would mention a bit about it:

you can have your string on multiple lines + have variable parsing

so that your query is much more readable:

"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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM