str_to_date函数在complexe mysql query joomla中不起作用

8wtpewkr  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(386)

我有一个sql查询,它返回3个最新的格式,但在我将fin\u date列类型从string更改为date并使用特定格式后,它就不起作用了:

$today = JFactory::getDate()->toFormat('%d/%m/%Y');
   // echo $today==> 18/12/2018

$query =  "SELECT p.fin_date, pfr.product_name, p.ville"
             ." FROM #__virtuemart_products as p"
             ." LEFT JOIN #__virtuemart_products_fr_fr as pfr on pfr.virtuemart_product_id = p.virtuemart_product_id"
             ." WHERE p.published = 1   AND  p.product_parent_id != 0 AND str_to_date(p.fin_date,'%d/%m/%Y') >= $today "
             ." LIMIT 4 "
             ;

甚至我把它改成:

$query =  "SELECT str_to_date(p.fin_date,'%d/%m/%Y'), pfr.product_name, p.ville"
             ." FROM #__virtuemart_products as p"
             ." LEFT JOIN #__virtuemart_products_fr_fr as pfr on pfr.virtuemart_product_id = p.virtuemart_product_id"
             ." WHERE p.published = 1   AND  p.product_parent_id != 0 AND str_to_date(p.fin_date,'%d/%m/%Y') >= $today "
             ." LIMIT 4 "
             ;
4jb9z9bj

4jb9z9bj1#

不能比较日期(的输出) str_to_date ),将在 Y-m-d 内部格式化为 d/m/Y 格式。你需要 $today 变量输入 Y-m-d 格式使用

$today = JFactory::getDate()->toFormat('%Y-%m-%d');

或者在查询中转换它。

str_to_date(p.fin_date,'%d/%m/%Y') >= str_to_date($today,'%d/%m/%Y')

相关问题