0

我首先创建一个包含 6 个标题/列的表,然后在其中添加一列并为该新列设置默认值。我要修改的部分是我设置的新列。我有一些默认值会改变的地方——基于一个不同的列,该列纯粹是我导入的数据。

我的代码如下:

SET GLOBAL local_infile=1;
CREATE TABLE audio(
    File_Name CHAR(4) PRIMARY KEY,
    File_Subdirectory CHAR(50),
    ....,
    ....,
    ....,
    ....,
);

-- Created table with the headers described above (left out 4 column names)

LOAD DATA LOCAL INFILE 'audio.csv'
     INTO TABLE audio
     FIELDS TERMINATED BY ',';

-- Loaded data into the right columns, even when I check with SELECT * FROM audio looks good

ALTER TABLE audio ADD COLUMN Microphone VARCHAR(15) DEFAULT 'Forward' AFTER File_Directory

-- Successful added a column after File_Directory with header Microphone and default was Forward for all entries/rows. 

IF (SELECT File_Directory FROM audio WHERE File_Driectory = 'Home')
    SET Microphone = 'Not Forward'
END IF;

-- Here I want to change the Microphone column (3rd column now - previously added and set default to be 'forward') to be 'Not Forward' if the File_Directory column has variable/entry to be home.

此外,如果我执行以下操作,我会得到我正在寻找的条目并且我想更改第三列。它打印出包含“Home”作为条目的多行的列。这些行是我想更改我添加的新列的行,我想将其从默认的“转发”更改为“不转发”。

SELECT File_Directory FROM audio WHERE File_Driectory = 'Home'

我的问题是最后一条语句,因为我无法设置或更改第三列的条目......这不是我不能,我不确定我在 IF 语句中做错了什么。我希望我提供的代码足以给我一个快速的解决方案。我省略了其他 4 个列名,因为它们不相关,我也没有在其中做任何事情。我再次从 csv 加载数据,成功添加了带有标题/和默认字段的列(这是大多数),我只需要更改新列需要具有不同名称/条目的几个地方。

任何的意见都将会有帮助!谢谢!

4

1 回答 1

1

I was able to solve this easily without even using an IF statement. I used the UPDATE command.

UPDATE audio SET Microphone = 'Not forward' WHERE File_Directory = 'Home'
于 2020-04-25T20:50:32.737 回答