sql 字符、數字類型自動轉換及運算


本頁面所有內容也可以在oracle 運行,只需要把int、float 、decimal 改為 number類型即可

-- 字符串轉數字 int 類型

drop table test;
create table test(id int);
insert into test values(100);
insert into test values('100');

-- 字符串轉數字 float 類型

drop table test;
create table test(id float(5,3));
insert into test values(10.99);
insert into test values('10.99');

-- 數字轉字符串類型

drop table test;
create table test(id varchar(5));
insert into test values(100);
insert into test values('100');

-- 加減運算

-- int類型可以參與加減運算

drop table test;
create table test(id int);
insert into test values(100);
select id-10 from test;

-- double類型可以參與加減運算

drop table test;
create table test(id double);
insert into test values(100);
select id-10 from test;

-- decimal類型可以參與加減運算

drop table test;
create table test(id decimal);
insert into test values(100);
select id-10 from test;

-- 字符串類型也可以參與加減運算

drop table test;
create table test(id varchar(5));
insert into test values(100);
select id-10 from test;  -- 字符串也可以減,mysql會自動轉型

-- 比較運算

-- int類型
drop table test;
create table test(id int);
insert into test values(100);
select * from test where id > 1;

drop table test;
create table test(id int);
insert into test values(100);
select * from test where id > '1';

-- 字符串類型

drop table test;
create table test(id varchar(5));
insert into test values(100);
select * from test where id > '1';

drop table test;
create table test(id varchar(5));
insert into test values(100);
select * from test where id > '中華人民共和國'; -- 語法正確,只是沒有結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM