开发者

Decatenate with MySQL?

开发者 https://www.devze.com 2023-02-16 07:22 出处:网络
I have an authors table in my database that lists an 开发者_如何转开发author\'s whole name, e.g. \"Charles Dickinson\".I would like to sort of \"decatenate\" at the space, so that I can get \'Charles\

I have an authors table in my database that lists an 开发者_如何转开发author's whole name, e.g. "Charles Dickinson". I would like to sort of "decatenate" at the space, so that I can get 'Charles" and "Dickinson" separately. I know there is the explode function in PHP, but is there anything similar for a straight mysql query? Thanks.


No, don't do that. Seriously. That is a performance killer. If you ever find yourself having to process a sub-column (part of a column) in some way, your DB design is flawed. It may well work okay on a home address book application or any of myriad other small databases but it will not be scalable.

Store the components of the name in separate columns. It's almost invariably a lot faster to join columns together with a simple concatenation (when you need the full name) than it is to split them apart with a character search.

If, for some reason you cannot split the field, at least put in the extra columns and use an insert/update trigger to populate them. While not 3NF, this will guarantee that the data is still consistent and will massively speed up your queries. You could also ensure that the extra columns are lower-cased (and indexed if you're searching on them) at the same time so as to not have to fiddle around with case issues.


This is related: MySQL Split String

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号