Zend_Db 按字段值排序
我使用以下命令从模型输出选择菜单的内容:
$select = $this->select();
$select->order('name');
return $this->fetchAll($select);
但是,我想要做的是按特定值排序,然后按名称列排序。 SQL 如下所示:
SELECT * FROM `names` ORDER BY `name` = 'SomeValue' DESC,`name`
示例 SQL 代码:
CREATE TABLE IF NOT EXISTS `names` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
INSERT INTO `names` (`id`, `name`) VALUES
(1, 'rob'),
(2, 'dave'),
(3, 'andy'),
(4, 'paul'),
(5, 'jason'),
(6, 'john');
SELECT *
FROM `names`
ORDER BY `name` = 'john' DESC , `name`
返回:
6 john
3 andy
2 dave
5 jason
4 paul
1 rob
I'm outputting the contents of a select menu from a model using this:
$select = $this->select();
$select->order('name');
return $this->fetchAll($select);
However, what i want to do is order by a specific value, and then by the name column. The SQL would look like this:
SELECT * FROM `names` ORDER BY `name` = 'SomeValue' DESC,`name`
SAMPLE SQL CODE:
CREATE TABLE IF NOT EXISTS `names` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
INSERT INTO `names` (`id`, `name`) VALUES
(1, 'rob'),
(2, 'dave'),
(3, 'andy'),
(4, 'paul'),
(5, 'jason'),
(6, 'john');
SELECT *
FROM `names`
ORDER BY `name` = 'john' DESC , `name`
RETURNS:
6 john
3 andy
2 dave
5 jason
4 paul
1 rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这就是您正在寻找的:
I believe this is what you are looking for:
不支持此操作,具体取决于您的 SQL 数据库。 (我不知道可以直接按字符串排序的数据库。)
您想完成什么?在第一个位置有具有 someValue 的字段
depending on your SQL DB, this is not supported. (i dont know a database which can sort directly by a string.)
what would you want to accomplish ? have fields with someValue at the first positions