Split value from one field to two

前端 未结 14 2127
面向向阳花
面向向阳花 2020-11-22 03:49

I\'ve got a table field membername which contains both the last name and the first name of users. Is it possible to split those into 2 fields memberfirst<

相关标签:
14条回答
  • 2020-11-22 04:12

    This takes smhg from here and curt's from Last index of a given substring in MySQL and combines them. This is for mysql, all I needed was to get a decent split of name to first_name last_name with the last name a single word, the first name everything before that single word, where the name could be null, 1 word, 2 words, or more than 2 words. Ie: Null; Mary; Mary Smith; Mary A. Smith; Mary Sue Ellen Smith;

    So if name is one word or null, last_name is null. If name is > 1 word, last_name is last word, and first_name all words before last word.

    Note that I've already trimmed off stuff like Joe Smith Jr. ; Joe Smith Esq. and so on, manually, which was painful, of course, but it was small enough to do that, so you want to make sure to really look at the data in the name field before deciding which method to use.

    Note that this also trims the outcome, so you don't end up with spaces in front of or after the names.

    I'm just posting this for others who might google their way here looking for what I needed. This works, of course, test it with the select first.

    It's a one time thing, so I don't care about efficiency.

    SELECT TRIM( 
        IF(
            LOCATE(' ', `name`) > 0,
            LEFT(`name`, LENGTH(`name`) - LOCATE(' ', REVERSE(`name`))),
            `name`
        ) 
    ) AS first_name,
    TRIM( 
        IF(
            LOCATE(' ', `name`) > 0,
            SUBSTRING_INDEX(`name`, ' ', -1) ,
            NULL
        ) 
    ) AS last_name
    FROM `users`;
    
    
    UPDATE `users` SET
    `first_name` = TRIM( 
        IF(
            LOCATE(' ', `name`) > 0,
            LEFT(`name`, LENGTH(`name`) - LOCATE(' ', REVERSE(`name`))),
            `name`
        ) 
    ),
    `last_name` = TRIM( 
        IF(
            LOCATE(' ', `name`) > 0,
            SUBSTRING_INDEX(`name`, ' ', -1) ,
            NULL
        ) 
    );
    
    0 讨论(0)
  • 2020-11-22 04:13

    The only case where you may want such a function is an UPDATE query which will alter your table to store Firstname and Lastname into separate fields.

    Database design must follow certain rules, and Database Normalization is among most important ones

    0 讨论(0)
  • 2020-11-22 04:14

    If your plan is to do this as part of a query, please don't do that (a). Seriously, it's a performance killer. There may be situations where you don't care about performance (such as one-off migration jobs to split the fields allowing better performance in future) but, if you're doing this regularly for anything other than a mickey-mouse database, you're wasting resources.

    If you ever find yourself having to process only part of a column in some way, your DB design is flawed. It may well work okay on a home address book or recipe application or any of myriad other small databases but it will not be scalable to "real" systems.

    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.

    And, if you cannot even add the columns and triggers, be aware (and make your client aware, if it's for a client) that it is not scalable.


    (a) Of course, if your intent is to use this query to fix the schema so that the names are placed into separate columns in the table rather than the query, I'd consider that to be a valid use. But I reiterate, doing it in the query is not really a good idea.

    0 讨论(0)
  • 2020-11-22 04:19

    SELECT variant (not creating a user defined function):

    SELECT IF(
            LOCATE(' ', `membername`) > 0,
            SUBSTRING(`membername`, 1, LOCATE(' ', `membername`) - 1),
            `membername`
        ) AS memberfirst,
        IF(
            LOCATE(' ', `membername`) > 0,
            SUBSTRING(`membername`, LOCATE(' ', `membername`) + 1),
            NULL
        ) AS memberlast
    FROM `user`;
    

    This approach also takes care of:

    • membername values without a space: it will add the whole string to memberfirst and sets memberlast to NULL.
    • membername values that have multiple spaces: it will add everything before the first space to memberfirst and the remainder (including additional spaces) to memberlast.

    The UPDATE version would be:

    UPDATE `user` SET
        `memberfirst` = IF(
            LOCATE(' ', `membername`) > 0,
            SUBSTRING(`membername`, 1, LOCATE(' ', `membername`) - 1),
            `membername`
        ),
        `memberlast` = IF(
            LOCATE(' ', `membername`) > 0,
            SUBSTRING(`membername`, LOCATE(' ', `membername`) + 1),
            NULL
        );
    
    0 讨论(0)
  • 2020-11-22 04:19

    It seems that existing responses are over complicated or not a strict answer to the particular question.

    I think, the simple answer is the following query:

    SELECT
        SUBSTRING_INDEX(`membername`, ' ', 1) AS `memberfirst`,
        SUBSTRING_INDEX(`membername`, ' ', -1) AS `memberlast`
    ;
    

    I think it is not necessary to deal with more-than-two-word names in this particular situation. If you want to do it properly, splitting can be very hard or even impossible in some cases:

    • Johann Sebastian Bach
    • Johann Wolfgang von Goethe
    • Edgar Allan Poe
    • Jakob Ludwig Felix Mendelssohn-Bartholdy
    • Petőfi Sándor
    • 黒澤

    In a properly designed database, human names should be stored both in parts and in whole. This is not always possible, of course.

    0 讨论(0)
  • 2020-11-22 04:19

    In MySQL this is working this option:

    SELECT Substring(nameandsurname, 1, Locate(' ', nameandsurname) - 1) AS 
           firstname, 
           Substring(nameandsurname, Locate(' ', nameandsurname) + 1)    AS lastname 
    FROM   emp  
    
    0 讨论(0)
提交回复
热议问题