简体   繁体   中英

Load data local infile with IF statement

I have table with data:

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

I have file, that I need to load into this table and replace:

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

I have one condition: if status in table =2 and status in file =0, leave status in table =2, otherwise replace status in table from file.
After query I need to get new data:

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

I'm trying do it with query:

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  

After that I get status fields NULL.
How to resolve this problem?

Edit:
I tried:

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),  

But result status 2 changed to 0.

Table schema:

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;

Real data:

| 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   |

File data:

| 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   |  

My temporary solution:

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));

I think this should suffice:

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;

If this doesn't help, you can try with the values() function , although it says that it's for the INSERT ... ON DUPLICATE KEY UPDATE statement.

In an INSERT ... ON DUPLICATE KEY UPDATE statement, you can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the statement. In other words, VALUES(col_name) in the UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. The VALUES() function is meaningful only in the ON DUPLICATE KEY UPDATE clause of INSERT statements and returns NULL otherwise. See Section 13.2.5.3, “INSERT ... ON DUPLICATE KEY UPDATE Syntax”.

If that doesn't help either, please provide the table schema and so on, so we can have a try ourselfs and don't have to guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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