MySQL查询以更改单列中的名称顺序

huangapple go评论126阅读模式
英文:

MySQL Query to Change Name Order in Single Column

问题

我有一个包含多个列的数据库,但我被要求按名字的顺序对数据进行排序。

问题在于姓和名都位于同一列中,标题为'owner',并且要求我不要创建额外的列来拆分值。

例如,数据看起来像这样,有列:

 id | owner               | animal
  1 | Rogers, Zachary     | Dog
  2 | Guttierez, Rob      | Cat
  3 | Peters, Melissa     | Bird
  4 | Angelou, Bernadette | Dog

我该如何将"姓, 名" 转换为"名 姓",以便进行排序?最后,我希望数据看起来像这样:

  1 | Zachary Rogers     | Dog
  2 | Rob Guttierez      | Cat
  3 | Melissa Peters     | Bird
  4 | Bernadette Angelou | Dog

在格式化完成后,我可以处理ORDER BY owner ASC;查询。

英文:

I have a database with multiple columns, but I have been tasked with ordering the data by first name.

Issue is that both the last name and first name reside in the same column, titled 'owner', and I've been asked to not create extra columns to split the values.

For instance, the data looks something like this, with columns

 id | owner               | animal
  1 | Rogers, Zachary     | Dog
  2 | Guttierez, Rob      | Cat
  3 | Peters, Melissa     | Bird
  4 | Angelou, Bernadette | Dog

How can I take "last name, first name" and make it "first name last name" so I can order it? At the end, I'd like the data to look like this:

  1 | Zachary Rogers     | Dog
  2 | Rob Guttierez      | Cat
  3 | Melissa Peters     | Bird
  4 | Bernadette Angelou | Dog

I can handle the ORDER BY owner ASC; query after the formatting is good.

TIA!

答案1

得分: 0

你需要使用SUBSTRING_INDEX函数来拆分owner。这个示例获取最后一个拆分索引,并按其进行排序。

SELECT * FROM table ORDER BY SUBSTRING_INDEX(owner, ' ', -1) ASC;

英文:

Your going to need to split owner using SUBSTRING_INDEX function. This example gets the last split index and sorts on that.

SELECT * FROM table ORDER BY SUBSTRING_INDEX(owner, ' ', -1) ASC;

答案2

得分: 0

SUBSTRING_INDEX函数使用分隔符','将允许您按姓氏排序

在长期内,还要规范化您的表,以便将动物存储在单独的表中

另外,将姓氏保存在单独的列中会节省时间,因为字符串函数会占用时间,最好用于其他用途#

CREATE TABLE mytable
    (`id` int, `owner` varchar(19), `animal` varchar(4))
;
    
INSERT INTO mytable
    (`id`, `owner`, `animal`)
VALUES
    (1, 'Rogers, Zachary', 'Dog'),
    (2, 'Guttierez, Rob', 'Cat'),
    (3, 'Peters, Melissa', 'Bird'),
    (4, 'Angelou, Bernadette', 'Dog')
;

> status > 记录数:4 重复项:0 警告:0 >

SELECT id
  , CONCAT(SUBSTRING_INDEX(`owner`,',', -1),', ', SUBSTRING_INDEX(`owner`,',', 1)) sortname  
  ,`animal` 
  FROM mytable
ORDER BY sortname  DESC
英文:

SUBSTRING_INDEX with the delimiter ',' will give you the chance to sortby surname

Still on the long run normalize your table, so that you animals in a separate table

also saving surname in a separate columns would save time as string functions will take time, better spent#

CREATE TABLE mytable
    (`id` int, `owner` varchar(19), `animal` varchar(4))
;
    
INSERT INTO mytable
    (`id`, `owner`, `animal`)
VALUES
    (1, 'Rogers, Zachary', 'Dog'),
    (2, 'Guttierez, Rob', 'Cat'),
    (3, 'Peters, Melissa', 'Bird'),
    (4, 'Angelou, Bernadette', 'Dog')
;

> status
> Records: 4 Duplicates: 0 Warnings: 0
>

SELECT id
  , CONCAT(SUBSTRING_INDEX(`owner`,',', -1),' ', SUBSTRING_INDEX(`owner`,',', 1)) sortname  
  ,`animal` 
  FROM mytable
ORDER BY sortname  DESC
id sortname animal
1  Zachary Rogers Dog
2  Rob Guttierez Cat
3  Melissa Peters Bird
4  Bernadette Angelou Dog

fiddle

huangapple
  • 本文由 发表于 2023年2月24日 03:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定