Zend 框架 SQL 选择查询构造(ORDER BY)
我有一个带有 VARCHAR 列 url
的数据库。我想获取行,以便具有任何 url
值的行优先于其他行,但按 date
行排序(降序),因此 ORDER BY 'url' DESC, 'date' DESC 不起作用,因为它会首先按字母顺序对它们进行排序。基本上,它看起来像这样:
Table:
ID | Url | Date
1 | http://...| 1001
2 | | 1002
3 | | 1003
4 | http://...| 1005
5 | http://...| 1004
Sorted:
ID | Url | Date
4 | http://...| 1005
5 | http://...| 1004
1 | http://...| 1001
3 | | 1003
2 | | 1002
正确的 zend 框架方式(或至少是 SQL 查询)是什么?
I have a database with VARCHAR column url
. I would like to fetch rows so that the ones that have any url
value have priority over other rows, but are ordered by date
row (descending), so ORDER BY 'url' DESC, 'date' DESC
wouldn't work as it would order them alphabetically first. Basically, it would look something like this:
Table:
ID | Url | Date
1 | http://...| 1001
2 | | 1002
3 | | 1003
4 | http://...| 1005
5 | http://...| 1004
Sorted:
ID | Url | Date
4 | http://...| 1005
5 | http://...| 1004
1 | http://...| 1001
3 | | 1003
2 | | 1002
What would be the proper zend framework way (or at least SQL query) to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 SQL,您可以这样...
如果您在 url 字段上也允许空值,它会变得有点难看。
这主要检查 url 长度是否大于零或为空。如果是的话,他们就排在最后。
With SQL, you could so something like...
It gets kind of ugly if you allow null values on the url field as well.
This basically checks to see if the url length is greater than zero or is null. If they are, they are at the bottom of the sort.
您可以在 select 语句中使用
order by field('field_name', value) desc
。You could use
order by field('field_name', value) desc
in the select statement.