Zend Db 按自定义列表排序

发布于 2025-01-06 04:58:20 字数 525 浏览 2 评论 0原文

我想使用 Zend Db 对自定义列表中的数据库的某些行进行排序。

它们具有 page_type 字段,可以是 0、1、2 或 3,我希望它们按以下顺序排列:1,3,2,0

我发现可以使用 MySQL

SELECT id 
FROM table 
WHERE id IN (15,21,4,8) 
ORDER BY id = 15 DESC, id = 21 DESC, id = 4 DESC, id = 8 DESC

Zend Db 也可以吗?还是我应该在这里使用普通 MySQL?

这是我现在的代码:

$select = $this->select()
    ->order(array(
        'asc' => 'page_type'
    ));

I'd like to sort some rows of my database on a custom list with Zend Db.

They have the field page_type, which can be 0, 1, 2 or 3 and I'd like them in this order: 1,3,2,0

I have found that it is possible with MySQL:

SELECT id 
FROM table 
WHERE id IN (15,21,4,8) 
ORDER BY id = 15 DESC, id = 21 DESC, id = 4 DESC, id = 8 DESC

Is it also possible with Zend Db or should I just use plain MySQL here?

This is my code now:

$select = $this->select()
    ->order(array(
        'asc' => 'page_type'
    ));

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

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

发布评论

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

评论(2

云淡月浅 2025-01-13 04:58:20

可以将 page_type 更改为 ENUM('1','3','2','0') (注意顺序)。 MySQL 内部会将索引 0 链接到值 1,索引 1 链接到值 3,等等。

如果您随后按 page_type ASC 排序,它将按照您希望的顺序排序,因为它按 ENUM 键而不是实际文本排序每个键都与之相关。

根据 page_type 实际链接到的内容,您始终可以使用文本描述符,即 ENUM('news','something else',.....) - 您没有提供有关链接到的内容的信息,因此无法进一步提供建议。

有关枚举的更多信息可以在这里找到:http://dev.mysql. com/doc/refman/5.0/en/enum.html (向下滚动到段落开始的位置“ENUM 值根据”排序......

唯一的缺点是您需要更新表格每次添加新的 page_type 时的架构,但是我认为由于字段名称,这不会经常发生,所以这可能是一个可行的选择

It would be possible to change page_type to be ENUM('1','3','2','0') (pay attention to the order). Internally MySQL will link index 0 to value 1, index 1 to value 3, etc etc.

If you then order by page_type ASC it will order in the order you'd like it to as it orders by the ENUM key rather than the actual text which each key relates to.

Depending on what page_type actually links to you could always use text descriptors instead i.e. ENUM('news','something else',.....) - you've provided no information on what that links to so cannot advise further on that.

More information on enum can be found here : http://dev.mysql.com/doc/refman/5.0/en/enum.html (scroll down to where the paragraph starts "ENUM values are sorted according to "....

The only down side is that you will need to update the table schema every time you add a new page_type, however I would assume due to the field name this won't be a common occurance so this could be a viable option

韵柒 2025-01-13 04:58:20

您使用的是哪个版本的 Zend Framework?以下内容曾经在 1.1x 中工作:

$select->order(array(
    new Zend_Db_Expr("page_type = 1 DESC"),
    new Zend_Db_Expr("page_type = 3 DESC"),
    new Zend_Db_Expr("page_type = 2 DESC"),
    new Zend_Db_Expr("page_type = 0 DESC")
));

$select->order(array(
    new Zend_Db_Expr("page_type = 1 DESC, page_type = 3 DESC, page_type = 2 DESC, page_type = 0 DESC")
));

Zend_Db_Expr 每当您想要将文学字符串插入到查询中时很有用,但正如您可能知道的那样 - 在将文学字符串插入查询中时必须非常小心。因此,如果使用此解决方案,请正确执行验证/过滤/转义。

Which version of Zend Framework are you using? The below used to work in 1.1x:

$select->order(array(
    new Zend_Db_Expr("page_type = 1 DESC"),
    new Zend_Db_Expr("page_type = 3 DESC"),
    new Zend_Db_Expr("page_type = 2 DESC"),
    new Zend_Db_Expr("page_type = 0 DESC")
));

or

$select->order(array(
    new Zend_Db_Expr("page_type = 1 DESC, page_type = 3 DESC, page_type = 2 DESC, page_type = 0 DESC")
));

Zend_Db_Expr is useful whenever you want to insert a literary string into a query, but as you probably know - you have to be very careful when inserting literary strings into queries. So do your validation / filtering / escaping properly if using this solution.

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