postgresql查询mysql库_PG数据库基本命令——查询(笔记)

1、插入数据(insert 语句)

语法:

INSERT INTOTABLE_NAME (column1, column2, column3,...columnN)VALUES (value1, value2, value3,...valueN);

实例:

INSERT INTOemployees( ID, NAME, AGE, ADDRESS, SALARY)VALUES(1, 'Maxsu', 25, '海口市人民大道2880号', 109990.00),

(2, 'minsu', 25, '广州中山大道', 125000.00),

(3, '李洋', 21, '北京市朝阳区', 185000.00),

(4, 'Manisha', 24, 'Mumbai', 65000.00),

(5, 'Larry', 21, 'Paris', 85000.00);

2、查询数据(SELECT语句)

语法:

SELECT "column1", "column2"..."columnN" FROM"table_name";SELECT * FROM "table_name";

3、更新数据(UPDATE语句)

语法:

UPDATEtable_nameSET column1 = value1, column2 = value2...., columnN =valueNWHERE [condition];

4、删除数据(DELETE语句)

语法:

DELETE FROMtable_nameWHERE [condition];

实例:

DELETE FROMEMPLOYEESWHERE ID = 1;

5、ORDER BY子句

语法:

SELECT column-listFROMtable_name[WHERE condition]

[ORDER BY column1, column2, .. columnN] [ASC | DESC];

实例:

SELECT *

FROMEMPLOYEESORDER BY AGE ASC;

6、分组(GROUP BY子句)

语法:

SELECT column-listFROMtable_nameWHERE [conditions]

GROUP BYcolumn1, column2....columnNORDER BY column1, column2....columnN

实例:

SELECT NAME, SUM(SALARY)FROMEMPLOYEESGROUP BY NAME;

在上面的例子中,当我们使用GROUP BY NAME时,重复的名字数据记录被合并。 它指定GROUP BY减少冗余。

7、Having子句

语法:

SELECTcolumn1, column2FROMtable1, table2WHERE [conditions]

GROUP BYcolumn1, column2HAVING [conditions]

ORDER BY column1, column2

实例:

执行以下查询表“EMPLOYEES”中name字段值计数大于1的名称。

SELECT NAME,COUNT(NAME)FROMEMPLOYEESGROUP BY NAME HAVING COUNT (NAME) > 1;

postgresql查询mysql库_PG数据库基本命令——查询(笔记)_第1张图片

8、条件查询

条件查询有:

AND 条件

OR 条件

AND & OR 条件

NOT 条件

LIKE 条件

IN 条件

NOT IN 条件

BETWEEN 条件

1)AND条件

语法:

SELECTcolumn1, column2, ..... columnNFROMtable_nameWHERE [search_condition]

AND [search_condition];

实例:

SELECT *

FROMEMPLOYEESWHERE SALARY > 120000

AND ID <= 4;

2) OR条件

语法:

SELECTcolumn1, column2, ..... columnNFROMtable_nameWHERE [search_condition]

OR [search_condition];

实例:

SELECT *

FROMEMPLOYEESWHERE NAME = 'Minsu'

OR ADDRESS = 'Noida';

3)AND & OR条件

语法:

SELECTcolumn1, column2, ..... columnNFROMtable_nameWHERE [search_condition] AND [search_condition]

OR [search_condition];

实例:

SELECT *

FROMEMPLOYEESWHERE (NAME = 'Minsu' AND ADDRESS = 'Delhi')OR (ID>= 8);

4)NOT条件

语法:

SELECTcolumn1, column2, ..... columnNFROMtable_nameWHERE [search_condition] NOT [condition];

实例:

查询那些地址不为 NULL 的记录信息,执行以下查询:

SELECT *

FROMEMPLOYEESWHERE address IS NOT NULL ;

查询那些年龄不是21和24的所有记录,执行以下查询:

SELECT *

FROMEMPLOYEESWHERE age NOT IN(21,24) ;

5)LIKE条件

like 与 where 子句一起,用于从指定条件满足 like 条件的表中获取数据。

语法:

SELECTcolumn1, column2, ..... columnNFROMtable_nameWHERE [search_condition] LIKE [condition];

实例:

查询名字以 Ma 开头的数据记录,如下查询语句:

SELECT *

FROMEMPLOYEESWHERE NAME LIKE 'Ma%';

执行结果如下图:

postgresql查询mysql库_PG数据库基本命令——查询(笔记)_第2张图片

查询名字以su结尾的数据记录,如下查询语句:

SELECT *

FROMEMPLOYEESWHERE NAME LIKE '%su';

6)IN条件

语法:

SELECTcolumn1, column2, ..... columnNFROMtable_nameWHERE [search_condition] IN [condition];

实例:

查询employee表中那些年龄为19,21的员工信息,执行以下查询:

SELECT *

FROMEMPLOYEESWHERE AGE IN (19, 21);

7)NOT IN条件

语法:

SELECTcolumn1, column2, ..... columnNFROMtable_nameWHERE [search_condition] NOT IN [condition];

8)BETWEEN条件

语法:

SELECTcolumn1, column2, ..... columnNFROMtable_nameWHERE [search_condition] BETWEEN [condition];

实例:

SELECT *

FROMEMPLOYEESWHERE AGE BETWEEN 24 AND 27;

你可能感兴趣的:(postgresql查询mysql库_PG数据库基本命令——查询(笔记))