使用IF语句加载本地infile数据

时间:2014-12-30 09:49:56

标签: php mysql if-statement load-data-infile

我有数据表:

| id  | status |
+-----+--------+
|  1  |    1   |
|  2  |    1   |
|  3  |    0   |
|  4  |    2   |
|  5  |    2   |

我有文件,我需要加载到此表并替换:

| id | status |  
+----+--------+
|  1 |    1   |  
|  2 |    0   |  
|  3 |    0   |  
|  4 |    0   |  
|  5 |    1   | 

我有一个条件:如果table = 2中的状态和file = 0中的状态,则将状态保留在table = 2中,否则将表中的状态替换为file。
查询后我需要获取新数据:

| id  | status |  
+-----+--------+
|  1  |    1   |  
|  2  |    0   |  
|  3  |    0   |  
|  4  |    2   |  
|  5  |    1   |  

我正在尝试使用查询:

load data local
infile '".$file."'
replace
into table t1
fields terminated by ',' enclosed by '\"'
(@tid, 
teacher_name, 
email,
@pid,   
tca_form_type, 
prod_company, 
prod_name,
@stts)
set status = if((select status from (select status from t1 where teacher_id=@tid and prod_id=@pid) as tmp)=2  and @stts=0,status,@stts),
teacher_id = @tid, prod_id = @pid  

之后我将状态字段设为NULL 如何解决这个问题?

修改
我试过了:

set status = if((select @var:=status from (select status from t1 where teacher_id=@tid and prod_id=@pid) as tmp)=2 and @stts=0,@var,@stts),  

但结果状态2更改为0.

表架构:

CREATE TABLE `table` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`teacher_id` VARCHAR(20) NOT NULL COLLATE 'utf8_unicode_ci',
`status` INT(11) NULL DEFAULT NULL,
`prod_id` VARCHAR(10) NOT NULL COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`id`),
UNIQUE INDEX `teacher_id_UNIQUE` (`teacher_id`, `prod_id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT
AUTO_INCREMENT=2053;

真实数据:

| id  | teacher_id | status | prod_id |
+-----+------------+--------+---------+
|  1  |    a1      |    1   |    15   |
|  2  |    a1      |    1   |    16   |
|  3  |    a1      |    0   |    17   |
|  4  |    a2      |    2   |    16   |
|  5  |    a2      |    2   |    18   |
|  6  |    a3      |    0   |    15   |
|  7  |    a3      |    1   |    20   |

文件数据:

| teacher_id | status | prod_id |
+------------+--------+---------+
|    a1      |    0   |    15   |
|    a1      |    1   |    16   |
|    a1      |    0   |    17   |
|    a2      |    1   |    16   |
|    a2      |    0   |    18   |
|    a3      |    1   |    15   |
|    a3      |    1   |    20   |  

我的临时解决方案:

load data local
                infile '".$file."'
                into table table_tmp
                fields terminated by ',' enclosed by '\"'
                (teacher_id, 
                teacher_name, 
                email,
                prod_id,   
                tca_form_type, 
                prod_company, 
                prod_name,
                status);
INSERT INTO table
                    (teacher_id, teacher_name, email, status, prod_id, tca_form_type, prod_company, prod_name)
                    SELECT teacher_id, teacher_name, email, `status`, prod_id, tca_form_type, prod_company, prod_name FROM table_tmp
                ON DUPLICATE KEY UPDATE table.status = IF(table.status = 2 and VALUES(status) = 0, table.status, VALUES(status));

1 个答案:

答案 0 :(得分:1)

我认为这应该足够了:

load data local
infile '".$file."'
replace
into table t1
fields terminated by ',' enclosed by '\"'
(@tid, 
teacher_name, 
email,
@pid,   
tca_form_type, 
prod_company, 
prod_name,
@stts)
set status = if(status = 2 and @stts = 0, status, @stts),
teacher_id = @tid, prod_id = @pid;

如果这没有帮助,您可以尝试使用values() function,但它会说它是INSERT ... ON DUPLICATE KEY UPDATE声明。

  

在INSERT ... ON DUPLICATE KEY UPDATE语句中,您可以使用UPDATE子句中的VALUES(col_name)函数来引用语句的INSERT部分中的列值。换句话说,UPDATE子句中的VALUES(col_name)引用了将插入的col_name的值,没有发生重复键冲突。此功能在多行插入中特别有用。 VALUES()函数仅在INSERT语句的ON DUPLICATE KEY UPDATE子句中有意义,否则返回NULL。请参见第13.2.5.3节“INSERT ... ON DUPLICATE KEY UPDATE语法”。

如果这也没有帮助,请提供表格架构等等,这样我们就可以尝试自己,不必猜测。