英文:
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 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论