简体   繁体   English

连接名字和姓氏以形成全名字段

[英]Concatenate first name and last name to form a full name field

I know a select command for it but it's only temporary. 我知道一个select命令,但它只是暂时的。 What can i do so it would be permanent? 我能做什么才能永久性? So if theres a record where the firstname is John and the lastname is Smith, in the fullname column, automatically it will generate a data of that record to be John Smith. 因此,如果theres是一个记录,其中firstname是John,而lastname是Smith,则在fullname列中,它将自动生成该记录的数据为John Smith。

$querystring = "select concat(firstname,' ',lastname) as name from users";

Why do you need to do this? 你为什么需要这样做? You're denormalizing data by repeating it. 您通过重复数据来对数据进行非规范化。 I don't know the specifics of your application, but I believe it would be best to calculate the full name in your presentation layer. 我不知道您的应用程序的具体细节,但我相信最好计算您的表示层中的全名。

If you want some kind of "display name" field (like Outlook), that would still need to be assigned at user entry time so the user could choose. 如果您想要某种“显示名称”字段(如Outlook),仍需要在用户输入时分配,以便用户可以选择。

No point in doing this. 这样做没有意义。 You can grab the First Name and Last Name from the Database, and when you need to display Full Name, just concat the two of them. 您可以从数据库中获取名字和姓氏,当您需要显示全名时,只需连接其中两个。 Do not Repeat Data in your Database. 不要在数据库中重复数据。

Just use the update command: 只需使用update命令:

UPDATE users SET fullname = CONCAT(firstname, ' ', lastname);

Note that if a user ever changes their first or last name the full name field will become out of date. 请注意,如果用户更改了他们的名字或姓氏,则全名字段将变得过时。 You might be better off only concatenating them on SELECT. 你可能最好只在SELECT上连接它们。

$full-name = $first-name.' $ full-name = $ first-name。' '.$last-name; ”。$最后的名字; echo $ull-name echo $ ull-name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM