【學習筆記】PostgreSQL進階查詢之連接查詢和子查詢


這一節主要包含以下內容:

  • 內連接

  • 左外連接

  • 右外連接

  • 全連接

  • 跨連接

  • 子查詢

一、內連接

說明:


提取兩表中某列相同的數據,查詢兩個表提取表中內容。

語法:


SELECT table1.columns,table2.columns
FROM table1 
INNER JOIN table2 
ON table1.common_filed =table2.common_field;

示例:


select employees.name, employees.salary,department.dept

from employees

inner join department

on employees.id=department.id;

結果:

二、左外連接

說明:

左外連接返回從“ON”條件中指定的左側表中的所有行,只返回滿足條件的另一個表中的行。

語法:

SELECT table1.columns, table2.columns 
FROM table1 
LEFT OUTER JOIN table2 
ON table1.common_filed = table2.common_field;

示例:

select employees.name, employees.salary,department.dept
from employees
left outer join department
on employees.id=department.id;

結果:

三、右外連接

說明:

右外連接返回從“ON”條件中指定的右側表中的所有行,只返回滿足條件的另一個表中的行。

語法:

SELECT table1.columns, table2.columns 
FROM table1 
RIGHT OUTER JOIN table2 
ON table1.common_filed = table2.common_field;

示例:

select employees.name, employees.salary,department.dept
from employees
right outer join department
on employees.id=department.id;

結果:

四、全外連接

說明:

全外連接從左表和左表中返回所有行。 它將NULL置於不滿足連接條件的位置。

語法:

SELECT table1.columns, table2.columns 
FROM table1 
FULL OUTER JOIN table2 
ON table1.common_filed = table2.common_field;

示例:

select employees.name, employees.salary,department.dept
from employees
full outer join department
on employees.id=department.id;

結果:

五、交叉連接

說明:

將第一個表的每一行與第二個表的每一行相匹配。 它也被稱為笛卡爾積。 如果table1具有“x”行,而table2具有“y”行,則所得到的表將具有(x * y)行。

語法:

SELECT coloums 
FROM table1 
CROSS JOIN table2;

示例:

select employees.name, employees.salary,department.dept
from employees
cross join department;

結果:

六、子查詢

說明:

子查詢或內部查詢或嵌套查詢是一個PostgreSQL查詢中的查詢,它可以嵌入到WHERE子句中。

必須遵循如下規則:

  • 子查詢必須括在括號中。
  • 子查詢在SELECT子句中只能有一列,除非主查詢中有多個列用於比較其所選列的子查詢。
  • ORDER BY不能用於子查詢,盡管主查詢可以使用ORDER BY。 GROUP BY可用於執行與子查詢中的ORDER BY相同的功能。
  • 返回多行的子查詢只能與多個值運算符一起使用,例如:IN,EXISTS,NOT IN,ANY / SOME,ALL運算符。
  • BETWEEN運算符不能與子查詢一起使用; 但是,BETWEEN可以在子查詢中使用。

1.帶SELECT語句的子查詢

語法:

SELECT column_name [,column_name ]
FROM table1 [,table2 ]
WHERE column_name OPERATOR( 
        SELECT column_name [,column_name ]
        FROMtable1 [,table2 ]
        [WHERE]);

示例:

select * from employees
where id in (select id from employees where salary > 100000);

結果:

2.帶INSERT語句的子查詢

語法:

INSERT INTO table_name [(column1 [,column2 ])]
SELECT [* | column1 [,column2 ]
FROM table1 [,table2 ]
[WHERE VALUE OPERATOR ];

示例:

insert into employees1
select * from employees 
where id in (select id from employees);

結果:
直接將employees表中的數據復制進了employees1里面。(employees1是employees的備份)

3.帶UPDATE語句的子查詢

語法:

UPDATE table 
SET column_name =new_value
[WHERE OPERATOR [VALUE]
(SELECT COLUMN_NAME FROM TABLE_NAME)
[WHERE)];

示例:

update employees set salary=salary * 0.5
where age in (select age from employees1 where age > 22);

結果:
選擇employees1里面age>22的數據,在employees中乘以0.5

4.帶DELETE語句的子查詢

語法:

DELETE FROM TABLE_NAME
[WHERE OPERATOR [VALUE]
(SELECT COLUMN_NAME FROM TABLE_NAME)
[WHERE)];

示例:

delete from employees
where age in (select age from employees1 where age=24);

結果:


免責聲明!

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



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