在 MYSQL 中使用 CASE 函数和 Zend_DB_Expr

发布于 2024-10-06 03:35:23 字数 744 浏览 1 评论 0原文

我遇到了以下问题。在我的产品表中,我有两列

Date_start 和 Date_end (它们在我的表中都是 DATE 数据类型)。

我想检查当前日期是否在 Date_start 和 Date_end 之间,那么状态必须为“不可用”,否则状态必须为“可用”。

我如何在 Zend_Db_Expr 中解决这个问题?

我现在有以下查询。

$getProducts = $this->oSelect
                            ->from(array('p'=>'producten'))
                            ->columns(array('link' => "CONCAT('/t/', p.titel_key)"))
                            ->joinLeft(array('c'=>'categorie'),'p.categorie_id = c.id',array('cat_titel'=>'c.titel'))
                            ->joinLeft(array('sc'=>'subcategorie'),'p.subcategorie_id = sc.id', array('subcat_titel'=>'sc.titel'))
                            ->where('p.online = 1');

I'm stuck with the following problem. In my product table i've two columns

Date_start and Date_end (both of them are a DATE datatype in my table).

I want to check if the current date is between the Date_start and Date_end then the status must be 'not available', else it must have the status 'available'.

How can I fix that in Zend_Db_Expr?

I've now the following query.

$getProducts = $this->oSelect
                            ->from(array('p'=>'producten'))
                            ->columns(array('link' => "CONCAT('/t/', p.titel_key)"))
                            ->joinLeft(array('c'=>'categorie'),'p.categorie_id = c.id',array('cat_titel'=>'c.titel'))
                            ->joinLeft(array('sc'=>'subcategorie'),'p.subcategorie_id = sc.id', array('subcat_titel'=>'sc.titel'))
                            ->where('p.online = 1');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2024-10-13 03:35:23

在您的专栏中:

->columns(array('link' => "CONCAT('/t/', p.titel_key)",
  'status' => new Zend_Db_Expr("...")))

“CASE”应该看起来像这样(为了便于阅读,我将其分开);

CASE WHEN p.Date_end < NOW() AND p.Date_start > NOW() 
THEN 'not available' ELSE 'available' END

Zend_Db_Expr 将接受您提供的任何内容并按字面意思使用它。请记住,如果由于某种原因切换系统,任何数据库特定命令都可能会中断。

In your columns:

->columns(array('link' => "CONCAT('/t/', p.titel_key)",
  'status' => new Zend_Db_Expr("...")))

The 'CASE' should look something like this (i split it out for readability);

CASE WHEN p.Date_end < NOW() AND p.Date_start > NOW() 
THEN 'not available' ELSE 'available' END

Zend_Db_Expr will take anything you give it and use it literally. Just remember that any DB specific commands might break if for some reason you switch systems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文