doude1917 2014-12-30 09:49
浏览 114

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

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));
  • 写回答

1条回答 默认 最新

  • drouie2014 2014-12-30 10:55
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题