简体   繁体   English

MySQL 按列名替换表中的值

[英]MySQL Replace value in table by column name

I have a MySQL table that looks like this, where pKey is a primary_AI Key.我有一个看起来像这样的MySQL表,其中pKey是 primary_AI 键。

+------+-----------+----------+----------+
| pKey | firstName | LastName | Username |
+------+-----------+----------+----------+
|    1 | John      | Brown    | jBrown   |
|    2 | Jake      | Smith    | jSmith   |
|    3 | Mary      | Laurier  | mLaurier |
+------+-----------+----------+----------+

I want to create a query where it can replace the LastName attribute when given a firstName .我想创建一个查询,它可以在给定firstName时替换LastName属性。 For example, when firstName of John is supplied, for example, I want to change the last name to Wilson .例如,当提供JohnfirstName时,例如,我想将姓氏更改为Wilson

I wrote this query to replace (or automatically create the row if not exists) the last name when supplied a first name.我编写此查询是为了在提供名字时替换(或如果不存在则自动创建行)姓氏。

REPLACE into `myTableName` (LastName, Username) VALUES ("Wilson", "jWilson") WHERE firstName = "John";

I'm expecting this query to change the record of John Brown to John Wilson and the username of jBrown to jWilson .我期待这个查询将John Brown的记录更改为John Wilson并将 jBrown 的用户名jBrownjWilson If the row that has the first name of John doesn't exist, I also want it to create a new row with the value of John as the firstName and Wilson as the LastName as well as changing the username to jWilson .如果名字为John的行不存在,我还希望它创建一个新行,其中John的值为firstNameWilsonLastName ,并将用户名更改为jWilson

I know that I can do this with multiple queries, such as SELECT the first name, INSERT if it does not exist, or DELETE the record and INSERT it again if it exists, but I have many records I need to insert at once and want to do this in a single query.我知道我可以通过多个查询来做到这一点,例如SELECT名字,如果不存在则INSERT ,或者DELETE记录并在存在时再次INSERT ,但是我有很多记录需要一次插入并且想要在单个查询中执行此操作。

How can I fix this?我怎样才能解决这个问题?

This should do it:这应该这样做:

REPLACE into `myTableName` (Username) VALUES (CONCAT(SUBSTR(firstname,0,1),lastname))) WHERE firstName = "John";

Once it works the way you want it to, remove the WHERE一旦它按照您想要的方式工作,请删除 WHERE

To update the existing values, use an UPDATE要更新现有值,请使用UPDATE

UPDATE `myTableName` SET username =  LCASE(CONCAT(SUBSTR(firstname,0,1),lastname))) WHERE firstName = "John";

A kind of hacky way to do this since you don't want to add the unique constraint to the first_name column even though you're sure it's gonna be unique would be to get the pKey of the row where firstName = 'John', and if that doesn't exist, use the next value that's gonna be generate for pKey (since it's set as auto_increment) instead, and use on duplicate key update :一种 hacky 方法可以做到这一点,因为您不想将unique约束添加到 first_name 列,即使您确定它将是唯一的,将是获取 firstName = 'John' 的行的 pKey,并且如果不存在,请改用将为 pKey 生成的下一个值(因为它设置为 auto_increment),并on duplicate key update时使用:

INSERT INTO table_name
 select (coalesce((select pKey from table_name where firstName = 'John'),
 (SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_NAME = "table_name"))) as pKey,
 'John' as firstName,
 'Wilson' as lastName,
 'jWilson' as userName
on duplicate key update lastName = 'Wilson', userName = 'jWilson';

Fiddle 小提琴

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

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