Sql语句用的不多,一直记不住,今天就总结列下,以后就不用查来查去了。
假设有表格
tbl_A
id | ip | port | description |
0 | 127.0.0.1 | 23 | |
1 | 192.0.0.1 | 80 | |
2 | localhost | 8080 |
查询
查询一张表格所有数据:
select * from tbl_A
查询一张表格特定列数据:
select ip, port from tbl_A
查询一张表格特定列特定条件的数据:
select ip, port from tbl_A where ip='127.0.0.1' and port=23
插入
insert into tbl_A (id, ip, port, description)
values (1, '192.169.20.1', 56, '')
更新
update tbl_a
set ip = '127.0.0.1', port = 66
where (id = 0)
删除
delete from tbl_a
where (ip = '127.0.0.1') and (port = 66)
关联更新
tbl_B
id | name |
0 | name1 |
1 | name2 |
2 | name3 |
把记录tbl_A的ip更新到tbl_B的name, 条件tbl_A和tbl_B记录的ID相等的情况小
UPDATE tbl_b
SET name = tbl_a.ip
FROM tbl_a, tbl_b
WHERE (tbl_a.id = tbl_b.id)
UPDATE b
SET name = a.ip
FROM tbl_a AS a, tbl_b AS b
WHERE (a.id = b.id)
内连接查询
左外联结
右外连接
完全外连接
http://www.iteye.com/topic/185385