使用PostgreSQL以正确的顺序获取名称

news/2024/7/9 21:19:10 标签: python, java, postgresql

Western and Eastern name orders can be a PITA, particularly when titles and honorifics come into play.

西方和东方的名称顺序可以成为PITA,尤其是在冠名和荣誉称呼起作用时。

The application I’m currently working on is a database of articles from a military magazine. Most of the authors are Hungarians, meaning they have a reversed name order. Many of them are also holding military ranks which have to be displayed. Others are Westerners, some with ranks and honorifics, some without. Some of them are fighter pilots, and they use their nicknames (callsigns) in writing. Not all of them though. At the end of the day it’s a completely random heap of names, titles, nicknames and honorifics, and I have to put this into order.

我目前正在使用的应用程序是一个军事杂志的文章数据库。 大多数作者是匈牙利人,这意味着他们的名字顺序颠倒了。 他们中许多人还拥有必须显示的军事队伍。 其他人则是西方人,有些人具有行列和荣誉,有些则没有。 其中一些是战斗机飞行员,他们以书面形式使用昵称(呼号)。 虽然不是全部。 归根结底,这是一堆完全随机的名字,头衔,昵称和荣誉称号,我必须将其整理好。

Here are some examples:

这里有些例子:

Tőrös István is the editor-in-chief with a very basic Hungarian name: last name first, and first name last. A few other languages, for example Japanese or Korean also follow this reversed name order.

Tőrös István ( Tőrös István是主编,他的名字非常匈牙利语:姓氏在前,姓氏在后。 其他几种语言(例如日语或韩语)也遵循此相反的名称顺序。

Varga Csaba Béla is another Hungarian name, but with two given names (or as we call them, Christian names). We don’t call it a middle name like in English, but “second given name”.

Varga Csaba Béla是匈牙利的另一个名字,但有两个给定名称(或我们称之为基督教名称)。 我们不像英语那样称呼它为中间名,而是“第二个给定名称”。

Hadi "Dino" István is a Hungarian fighter pilot who used his nickname when signing his articles. Therefore it should be displayed.

Hadi "Dino" István ( Hadi "Dino" István是匈牙利的战斗机飞行员,在签署文章时使用了自己的昵称。 因此,应该显示它。

Dr. Kositzky Attila altbgy. is another military officer and pilot, but he used his scientific title and military rank (Brigadier General) and no nickname.

Dr. Kositzky Attila altbgy. 是另一名军官和飞行员,但他使用了自己的科学头衔和军衔(准将),没有昵称。

(I wonder how long it will take for some of them to find this article and wonder how their names became examples in a coding tutorial.)

(我想知道其中一些人需要多长时间才能找到本文,并且不知道他们的名字在编码教程中如何成为示例。)

This diversity may already be enough to make your head spin, but let’s throw a few foreigners into the mix:

这种多样性可能已经足以使您旋转,但让我们将一些外国人加入其中:

Anatoly Nikolaevich Kvochur is Russian, so he of has a “middle name” called a patronymic. He’s also a military officer, but doesn’t use his rank when writing.

Anatoly Nikolaevich Kvochur是俄罗斯人,因此他有一个“中间名”,称为“赞助人”。 他也是军官,但写作时不使用职级。

Gen. Chuck Yeager does though. He has a Western name, first name and last name in “proper” order (for Westerners at least) and here we have the rank before the name, not after it like for Hungarians.

Gen. Chuck Yeager 。 他有一个西方名字,名字和姓氏(至少对于西方人来说),按“适当的”顺序排列,在这里,我们在名字前拥有排名,而匈牙利人则没有。

Let’s bring a really fancy person into the pool, although he never wrote any articles for this magazine:The Right Honourable Mr. Justin Pierre James Trudeau, Prime Minister of Canada. This guy has not one, but three titles, and two middle names! Yikes! How would you handle that?!

让我们带一个真正想像的人,尽管他从未为该杂志写过任何文章: The Right Honourable Mr. Justin Pierre James Trudeau, Prime Minister of Canada. 这个家伙没有一个,而是三个头衔和两个中间名! kes! 您将如何处理?

If your application is English only, it’s a little bit easier. As a general rule, foreign names in English should be written using Western name order. For example my own name would be Tamas Polgar and not Polgár Tamás. In many other languages however, including Hungarian, the original name order is retained, so Donald Trump will never become Trump Donald. Titles and ranks will still follow Hungarian customs though: President Donald Trump will be Donald Trump elnök. Let’s just ignore the problem of translating titles and ranks for now.

如果您的应用程序仅是英语,那会容易一些。 通常,英语的外国名称应使用西方名称顺序书写。 例如,我自己的名字叫Tamas Polgar而不是Polgár Tamás 。 但是,在许多其他语言中,包括匈牙利语,都保留了原始名称顺序,因此Donald Trump永远不会成为Trump Donald 。 头衔和职级仍然会遵循匈牙利的习俗: President Donald Trump将是Donald Trump elnök 。 现在让我们忽略翻译标题和等级的问题。

The solution

解决方案

To retrieve names in correct order you can code some lengthy processing function in your backend, but it’s easier to teach PostgreSQL to do it for you.

要以正确的顺序检索名称,您可以在后端中编写一些冗长的处理函数,但是教PostgreSQL为您执行此操作会更容易。

Here is the structure of my authors table:

这是我的authors表的结构:

CREATE TABLE IF NOT EXISTS topgun.authors (
id serial NOT NULL PRIMARY KEY,
title character varying NOT NULL DEFAULT '',
first_name character varying NOT NULL,
middle_name character varying NOT NULL DEFAULT '',
last_name character varying NOT NULL,
rank character varying NOT NULL DEFAULT '',
nickname character varying NOT NULL DEFAULT '',
name_order numeric NOT NULL DEFAULT 0
);

Only first_name and last_name are mandatory. The name_order field is a flag with the value of 0 for Eastern and 1 for Western name order. We are going to ignore multiple middle names because technically they’re one string, so a single field, middle_name will serve just fine for them. The same goes for multiple titles and ranks. We have title and rank separated because the first will always stay at the beginning of the name (Mr., Dr., etc.) but the other one may vary.

只有first_namelast_name是必需的。 name_order字段是一个标志,东方名称顺序为0 ,西方名称顺序为1 。 我们将忽略多个中间名,因为从技术上讲它们是一个字符串,因此单个字段middle_name将对它们middle_name 。 多个标题和等级也是如此。 我们将titlerank分开,因为第一个始终会保留在名称的开头(先生,博士等),但是另一个可能会有所不同。

Here’s how we’ll retrieve the names.

这是我们检索名称的方法。

Western order:Long: title rank first_name middle_name nickname last_nameShort: first_name middle_name last_name

西方顺序:长: title rank first_name middle_name nickname last_name短: first_name middle_name last_name

Eastern order:Long: title, last_name, nickname, middle_name, first_name, rankShort: last_name, first_name, middle_name

东部顺序:长: title, last_name, nickname, middle_name, first_name, rank短:首名last_name, first_name, middle_name

Let’s create or query:

让我们创建或查询:

SELECT
*
FROM topgun.authors
ORDER BY last_name, first_name, id

One of the reasons to store first, middle and last names separately is to simplify ordering, but it’ll also come handy when implementing a search function.

分别存储名字,中间名和姓氏的原因之一是简化排序,但是在实现搜索功能时也将很方便。

Now let’s extend our query with a conditional.

现在让我们用条件查询扩展查询。

SELECT
*, (CASE WHEN(name_order = 1)
THEN
CONCAT(
(first_name || ' '),
(CASE WHEN (middle_name <> '')
THEN (middle_name || ' ')
END),
last_name
) ELSE
CONCAT(
(last_name || ' '),
(first_name),
(CASE WHEN (middle_name <> '')
THEN (' ' || middle_name)
END)
)
END) AS nameFROM topgun.authors
ORDER BY last_name, first_name, id

The result will now contain a field called name which contains the name of the person in proper order.

现在,结果将包含一个名为name的字段,其中包含按正确顺序排列的人的姓名。

CONCAT means string concatenation. In Western name order, which is the first branch of the CASE WHEN condition, it concatenates first_name, middle_name and last_name. There’s another CASE WHEN in it because we don’t want to add the middle name if the person doesn’t have one. The same happens in the ELSE branch, which means Eastern name order, but the fields are concatenated in a different order. In both cases the field values are also concatenated with a space character either before or after the value, as required.

CONCAT表示字符串连接。 按照西方名称顺序(这是CASE WHEN条件的第一个分支),它连接first_namemiddle_namelast_name. 其中还有另一种CASE WHEN ,因为如果此人没有名字,我们不想添加中间名。 在ELSE分支中也会发生同样的情况,这意味着东部名称顺序,但是字段以不同顺序连接在一起。 在这两种情况下,都可以根据需要在值之前或之后用空格字符将字段值连接在一起。

The full name of the person with titles, nicknames, etc. works the same way, but it’s a little more complicated. I’m only showing the conditional here:

具有标题,昵称等的人的全名以相同的方式工作,但要复杂一些。 我只在这里显示条件:

(CASE WHEN (name_order = 1)
THEN
CONCAT(
(CASE WHEN (title <> '')
THEN (title || ' ')
END),
(CASE WHEN (rank <> '')
THEN (rank || ' ')
END),
(first_name || ' '),
(CASE WHEN (middle_name <> '')
THEN (middle_name || ' ')
END),
(CASE WHEN (nickname <> '')
THEN ('"' || nickname || '" ')
END),
last_name
) ELSE
CONCAT(
(CASE WHEN (title <> '')
THEN (title || ' ')
END),
(last_name || ' '),
(CASE WHEN (nickname <> '')
THEN ('"' || nickname || '" ')
END),
(CASE WHEN (middle_name <> '')
THEN (middle_name || ' ')
END),
first_name,
(CASE WHEN (rank <> '')
THEN (' ' || rank)
END)
)
END) AS full_name

Aaand magic! The query now returns full names in proper name order! Nicknames will be displayed in quotes.

阿兰德魔术! 查询现在以正确的名称顺序返回全名! 昵称将显示在引号中。

If you let your fields take a NULL value, simply add IS NOT NULL to your conditions like this:

如果让您的字段采用NULL值,只需将IS NOT NULL添加到您的条件中,如下所示:

(CASE WHEN (middle_name IS NOT NULL AND middle_name <> '')
THEN (middle_name || ' ')
END),

Of course this can be polished even further, but you probably got the idea.

当然这可以进一步完善,但是您可能知道了。

翻译自: https://medium.com/developer-rants/getting-names-in-the-right-order-with-postgresql-704104cf8fde


http://www.niftyadmin.cn/n/952894.html

相关文章

unity跑酷怎么添加金币_叫好不叫买?《跑酷老奶奶》游戏评测

游戏简述&#xff1a;游戏背景讲述的是一位名叫史密斯的老奶奶&#xff0c;她非常喜欢苹果,但是有一个配备滚轮装备的小偷正从她的果园里偷苹果!为了抓住小偷&#xff0c;她从果园追逐到城市&#xff0c;飞跃谷仓和办公室&#xff0c;务必在小偷得手之前,先获得苹果&#xff01…

spotify mp3_我的乐高人仔玩spotify

spotify mp3Instead of selecting a record, a minifigure is selected for this Jukebox Portal.无需选择记录&#xff0c;而是为此Jukebox Portal选择了一个小人像。 The “toys-to-life” LEGO Dimensions console game was discontinued in October 2017. Now you can brin…

滴滴人脸识别申诉照片怎么拍_好看人像写真怎么拍?10个简单姿势拿去拍,人像照片张张都漂亮...

很多女孩子都希望能够趁着自己年轻&#xff0c;拍一些好看的人像写真&#xff0c;这样不管到什么时候都可以欣赏自己年轻漂亮美美的样子&#xff0c;但是很多时候&#xff0c;大家并不知道应该怎么拍人像写真才好看&#xff0c;所以&#xff0c;今天我们就来说说10个简单的写真…

程序员用学位证吗_如何成为没有学位或新兵训练的开发人员

程序员用学位证吗Preface: This post is geared towards people interested in being self-taught because boot-camps or college aren’t viable options for them for whatever reason. As a self-taught developer myself, this is the path that I have experience with a…

如何用smardaten90天快速开发并上线智慧空间loT物联平台?

前言 大家好&#xff0c;我是小白白&#xff0c;前段时间一位好友接手了一个“烫手山芋”开发任务&#xff0c;77万㎡的科技园区需要打造智慧空间物联平台。要求接入600园区设备&#xff0c;处理15000日数据量&#xff0c;在打造整体IOT物联底座之上&#xff0c;构建起整个园区…

自学小程序开发_最有效的自学程序员开发人员的5个习惯

自学小程序开发Programming is one of the easiest professions to take without any previous training or background. But when you are an entirely self-taught programmer/developer you can end up with some bad habits that are some times hard to kick off.编程是没…

530黑苹果硬解_苹果手机软解和硬解的区别

苹果手机出现激活锁很多人去搜索答案&#xff0c;或者去问手机维修店的人&#xff0c;没有一人能准确说的让你明白清楚&#xff0c;还是那句老话&#xff0c;复杂的问题一句两句是说不清楚的&#xff0c;在你没搞懂之前不要心浮气躁&#xff0c;这次就用大白话给有疑问的用户理…

mvc应用程序生命周期_使用mvc设计模式制作出色的应用程序

mvc应用程序生命周期MVC设计模式的背景&#x1f4da;(Background on the MVC Design Pattern&#x1f4da;) Tightly coupled code can be easy to write but is a huge pain in the A** when trying to scale…紧密耦合的代码易于编写&#xff0c;但是在尝试扩展时却给A **带来…