简体   繁体   English

具有名称,名字和姓氏的PostgreSQL多表查询

[英]PostgreSQL multi-table query with name, first_name, and last_name

I have two tables in my PostgreSQL database. 我的PostgreSQL数据库中有两个表。

One table, call it 'Archive', has a column 'name' which are two-word first-name and last-name, eg 'John Smith' and an empty column which wants to be full of 'user_id'. 一个名为“ Archive”的表具有一个名为“ name”的列,该列是两个单词的名字和姓氏,例如“ John Smith”,还有一个空列,希望填充“ user_id”。

The second table, call it 'User Accounts', has three columns, one 'first_name' and one 'last_name' and one 'id'. 第二个表称为“用户帐户”,具有三列,一列为“ first_name”,一列为“ last_name”,一列为“ id”。

My goal is to implement a query that uses the column in Archive to select for the corresponding first_name and last_name in User Accounts and return to Archive the 'id' into 'user_id' column 我的目标是实现一个查询,该查询使用“存档”中的列为用户帐户中的相应first_name和last_name进行选择,然后将“ id”返回到“ user_id”列中进行存档

You can use a join . 您可以使用join For instance: 例如:

select a.*, ua.id as user_id
from archive a left join
     user_accounts ua
     on a.name = ua.first_name || ' ' || ua.last_name;

If you actually want to update the column: 如果您实际上要更新列:

update archive a 
    set user_id = ua.id
    from user_accounts ua
    where a.name = ua.first_name || ' ' || ua.last_name;

Note that name matching can be quite tricky. 请注意,名称匹配可能非常棘手。 You may not get as many matches as you expect. 您可能不会获得预期的匹配数。 If this turns out to be an issue, ask another question . 如果发现这是一个问题,请问另一个问题 Include sample data and desired results in the question. 在问题中包括样本数据和所需结果。

暂无
暂无

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

相关问题 通过First_Name和/或Last_Name查询sql表 - query sql table By First_Name and/or Last_Name 如何使用sql查询组合first_name和last_name? - How to combined first_name and last_name using sql query? 查询以使用外键获取行值(first_name 和 last_name) - Query to fetch row values (first_name & last_name) using foreign key 如有空格,如何将NAME分为3个不同的字段FIRST_NAME,MIDDLE_NAME和LAST_NAME - How to split NAME into 3 different fields FIRST_NAME, MIDDLE_NAME & LAST_NAME when there is a space 试图在 Oracle SQL 中将 FULL_NAME 分成 FIRST_NAME 和 LAST_NAME,但它没有显示表本身的任何更改 - Trying to separate a FULL_NAME into FIRST_NAME and LAST_NAME in Oracle SQL, but it does not show any changes on the table itself 如何编写查询以获取订单包含超过 2 件商品的客户的名字和姓氏? - How do I write a query to get the first_name and last_name of customers having orders containing more than 2 items? 使用 Reg_exp 从电子邮件预言机中提取名字和姓氏 - Extracting First_name & Last_name from email oracle using Reg_exp 表示 Last_Name + First_Name 有一个具有特定值的记录 - Indicate a Last_Name + First_Name has one record with specific value 在本地数据库中存储 OAuth 用户信息(用户名、名字、姓氏等) - Storing OAuth user info (username, first_name, last_name etc) in local database PHP-如何在登录后获取正确的名字和姓氏? - PHP - How to get correct first_name and last_name after login?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM