MySQL 删除 order by 子句之前的空格

发布于 2024-09-14 10:25:35 字数 289 浏览 6 评论 0原文

我有一张满是“标题”的桌子,我想按标题排序。问题是很多标题前都有空格。我正在考虑编写一个 php 脚本来解决这一切(超级简单),但我很好奇我该怎么做:

SELECT * FROM products ORDER BY title

但同时修剪标题,这样它就不会在空白处排序。所有这些都在同一个查询中,无需更改数据。天哪,我希望我说得有道理。

所以我真的正在寻找一个 mysql 解决方案。就其价值而言,我使用 Zend_Db,因此使用它的解决方案会更好,但我可以直接管理 MySQL。

I have a table full of "title" that I want to order by title. Problem is a lot of them have a empty space before the title. I am thinking of writting a php script to fix it all (super easy) but I was curious how could I do:

SELECT * FROM products ORDER BY title

But at the same time trimming the title so it doesnt order on the empty space. All in the same query without changing the data. God I hope I make sense.

So I am really looking for a mysql solution to this. For what its worth I use Zend_Db so a solution using that would be even better but I can manage straight MySQL.

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

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

发布评论

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

评论(3

青萝楚歌 2024-09-21 10:25:35

您可以使用 TRIM 功能:

SELECT TRIM(title) as title, field2, field3 FROM products ORDER BY TRIM(title)

应该可以了!

You can use the TRIM function:

SELECT TRIM(title) as title, field2, field3 FROM products ORDER BY TRIM(title)

That ought to do it!

爱要勇敢去追 2024-09-21 10:25:35

我会回答自己,因为我的问题的确切解决方案是:

SELECT * FROM products ORDER BY TRIM(title)

我仍然会接受科迪克里格的答案,因为他做到了,所以我找到了我的解决方案。

这是 Zend_Db 的答案:

$products->fetchAll($products->select()->order('TRIM(title) ASC'));

I'll answer myself because the exact solution to my question is:

SELECT * FROM products ORDER BY TRIM(title)

I'll still accept codykrieger's answer because he made it so I found my solution.

Here is the Zend_Db answer:

$products->fetchAll($products->select()->order('TRIM(title) ASC'));
少女情怀诗 2024-09-21 10:25:35

纯 SQL 查询如下所示:

更新产品 SET title = TRIM( title )

A pure SQL query would look like this:

UPDATE products SET title = TRIM( title )

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