---
title: "Sql语句示例"
author: "稻-草"
date: "2010-01-30T11:15:40Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2010-01-30T11:15:40Z"
id: 583827
url: "https://dcao.iteye.com/blog/583827"
---

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