简体   繁体   中英

MySQL: understanding sql code

I'm trying to understand some code created by someone who is not anymore working in this project, but I'm having some difficulties to understand the following line. I thought some one more experienced using mysql would be able to help me with this.

here it goes the line code:

SET price_calc=(REPLACE(@price,',','.') * IF(SUBSTRING(nmu,-1)='I', 1.414819,1.21))";

This SET is part of the LOAF DATA INFILE statement, and the part I don't understand is this expression:

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
    [TERMINATED BY 'string']
    [[OPTIONALLY] ENCLOSED BY 'char']
    [ESCAPED BY 'char']
]
[LINES
    [STARTING BY 'string']
    [TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...] -- this corresponds to the line that uses the expression I'm not understanding

Can you help me understanding what it is saying?

SET price_calc=(REPLACE(@price,',','.') * IF(SUBSTRING(nmu,-1)='I', 1.414819,1.21))";

The SET price_calc is going to set the value for a column named "price_calc". It will set it to the result of the calculation on the right-hand side of the "=".

This

(REPLACE(@price,',','.') * IF(SUBSTRING(nmu,-1)='I', 1.414819,1.21))

simplifies to

first-expression * second-expression

The first expression is

REPLACE(@price,',','.')

It replaces commas with periods in @price . Some locales use a comma as a decimal point; your database apparently does not.

The second expression is

IF(SUBSTRING(nmu,-1)='I', 1.414819,1.21)

That means you multiply first-expression by either 1.414819 or 1.21, depending on the value of SUBSTRING(nmu,-1) .

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