子串 + CakePHP 中虚拟字段的 Concat - 可能吗?
这似乎是一个简单的问题,但我无法让它发挥作用。我创建了一个结合了名称和地址的虚拟字段:
var $virtualFields = array(
'name_address' => 'CONCAT(Venue.name, " - ", Venue.address)'
);
有没有办法对此使用 SUBSTR
? (PHP 或 MYSQL 都可以 - 哪个最好/有效)。
我已经尝试过这个(无济于事):
'name_address' => 'SUBSTR(CONCAT(Venue.name, " - ", Venue.address),0,50)'
我还在 CONCAT 中的各个字段上尝试了 SUBSTR。
This seems like a simple question, but I can't get it to work. I've created a virtual field that combines a name and address:
var $virtualFields = array(
'name_address' => 'CONCAT(Venue.name, " - ", Venue.address)'
);
Is there a way to use SUBSTR
on this? (PHP or MYSQL is fine - whichever is best/works).
I've tried this (to no avail):
'name_address' => 'SUBSTR(CONCAT(Venue.name, " - ", Venue.address),0,50)'
I've also tried SUBSTR on the individual fields within the CONCAT.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这绝对没有问题。您唯一的错误是,您认为 MySQL 中的 SUBSTRING 函数就像 PHP 中的函数一样,其中计数从零开始。
但由于这不是 PHP,因此您必须从
1
开始计数,一切都会正常工作。只需像这样使用您的 virtualField ,它就应该像您想要的那样工作:
另外,请阅读 SUBSTRING 函数 MySQL 手册
This is absolutely no problem. Your only error is, that you thought of the SUBSTRING-function in MySQL working like the one in PHP where the count starts at zero.
But since this isn't PHP you have to start your counting from
1
and all will work fine.Just use your virtualField like this and it should work just like you want:
Also, have a read on the SUBSTRING-function in the MySQL manual