mysql常用sql語句記錄1

2022-10-10 19:06:11 字數 970 閱讀 7513

1.解決返回值為null的問題

//假設name,addr返回的值中可能存在null值

select id,name,addr from tabe1 where id=1;

//修改的後的sql為

select id,

case when name is null

then ''

else name

end as name,

case when addr is null

then ''

else addr

end as addr

from tabe_name where id=

1;

2.insert into select
//假設存在兩種表table1,table2

insert into table1 as t1 (id,name,addr)

values (1,

'lisi'

,(select addr from table2 t2 where t2.id in

(select id from table 3 where id>1)

))

3.對某個需要查詢的字段進行計算
//對查詢結果中的id欄位全部+1,

select id+

1 as id,name,addr from tabe1;

//還可以進行字串拼接

select id,name,addr,name+addr as n from tabe1;

4.insert into select計算
insert into table1 (age,num) 

select age+

1 as age,num+

2 as num from table2 where id=

3;

MySql 常用SQL語句

create database kali use kali show tables create table students sno varchar 10 primary key,sname varchar 10 not null,varchar 2 check in 男 女 age varcha...

常用sql語句(mysql)

給表新增列 sql alter table table name add column col name varchar 255 not null default default value 增加表列,指定格式 sql alter table table name add col name bool...

MySQL常用SQL語句

查詢一張表中的所有資料 sql view plain copy select from table name 查詢一張表中的指定列資料 sql view plain copy select column a,column b from table name 按條件查詢一張表中的所有資料 sql vi...