一、sql兩個表關聯查詢where
在進行兩個表關聯查詢時,where條件語句是必不可少的一部分。where語句可以用來過濾掉不需要的數據,達到快速準確的查詢的目的。
例如,我們有一張名為student的表,內容如下:
+----+-------+------+------+---------+ | id | name | age | city | country | +----+-------+------+------+---------+ | 1 | Tom | 20 | 北京 | 中國 | | 2 | Jack | 21 | 上海 | 中國 | | 3 | Lily | 20 | 北京 | 中國 | | 4 | Kate | 23 | 北京 | 美國 | | 5 | David | 22 | 上海 | 美國 | +----+-------+------+------+---------+
再有一張名為score的表,內容如下:
+----+---------+-------+ | id | student | score | +----+---------+-------+ | 1 | 1 | 90 | | 2 | 2 | 80 | | 3 | 3 | 95 | | 4 | 4 | 78 | | 5 | 5 | 87 | +----+---------+-------+
現在我們需要查詢出選了「北京」這個城市的學生的成績。
sql語句如下:
SELECT student.name, score.score FROM student, score WHERE student.id=score.student AND student.city="北京"
執行結果如下:
+------+-------+ | name | score | +------+-------+ | Tom | 90 | | Lily | 95 | | Kate | 78 | +------+-------+
二、sql關聯查詢兩個表
在進行兩個表關聯查詢時,我們需要用到join關鍵字。join關鍵字用於將兩個表進行連接,以實現數據查詢。
例如,我們需要查詢每個學生的成績,sql語句如下:
SELECT student.name, score.score FROM student INNER JOIN score ON student.id=score.student
執行結果如下:
+-------+-------+ | name | score | +-------+-------+ | Tom | 90 | | Jack | 80 | | Lily | 95 | | Kate | 78 | | David | 87 | +-------+-------+
三、sql兩個表關聯查詢顯示中文
sql兩表關聯查詢時,如果需要在結果集中顯示中文,需要對中文進行轉義。
例如,我們要查詢每個學生的單位和國籍,sql語句如下:
SELECT student.name, CONCAT(student.city,", ",student.country) as unit, score.score, score.date FROM student INNER JOIN score ON student.id=score.student
執行結果如下:
+-------+--------+-------+------------+ | name | unit | score | date | +-------+--------+-------+------------+ | Tom | 北京, 中國 | 90 | 2022-06-18 | | Jack | 上海, 中國 | 80 | 2022-06-18 | | Lily | 北京, 中國 | 95 | 2022-06-18 | | Kate | 北京, 美國 | 78 | 2022-06-18 | | David | 上海, 美國 | 87 | 2022-06-18 | +-------+--------+-------+------------+
四、sql兩個表關聯查詢分別查一部分
在兩個表關聯查詢時,有時候需要只查詢一個表中的部分數據,例如只查詢學生表中「北京」學生的成績。
sql語句如下:
SELECT student.name, score.score FROM student INNER JOIN score ON student.id=score.student WHERE student.city="北京"
執行結果如下:
+------+-------+ | name | score | +------+-------+ | Tom | 90 | | Lily | 95 | | Kate | 78 | +------+-------+
五、sql三個表關聯查詢
在進行三個表關聯查詢時,我們需要用到多個join關鍵字,以實現三個表之間的數據查詢。
例如,我們有一張名為teacher的表,內容如下:
+----+------+--------+ | id | name | course | +----+------+--------+ | 1 | 張老師 | 語文 | | 2 | 李老師 | 數學 | | 3 | 王老師 | 英語 | +----+------+--------+
現在我們需要查詢每個學生的成績和所選課程的老師的名字。
sql語句如下:
SELECT student.name, score.score, course.name, teacher.name FROM student, score, course, teacher WHERE student.id=score.student AND score.course=course.id AND course.teacher=teacher.id
執行結果如下:
+-------+-------+--------+--------+ | name | score | name | name | +-------+-------+--------+--------+ | Tom | 90 | 語文 | 張老師 | | Jack | 80 | 數學 | 李老師 | | Lily | 95 | 英語 | 王老師 | | Kate | 78 | 語文 | 張老師 | | David | 87 | 數學 | 李老師 | +-------+-------+--------+--------+
六、兩張表關聯查詢sql語句
在兩張表關聯查詢時,我們可以使用內連接(inner join)進行關聯查詢。
例如,我們查詢每個學生的成績和所在城市,sql語句如下:
SELECT student.name, student.city, score.score FROM student INNER JOIN score ON student.id=score.student
執行結果如下:
+-------+--------+-------+ | name | city | score | +-------+--------+-------+ | Tom | 北京 | 90 | | Jack | 上海 | 80 | | Lily | 北京 | 95 | | Kate | 北京 | 78 | | David | 上海 | 87 | +-------+--------+-------+
七、多表關聯查詢sql規則
在進行多表關聯查詢時,我們必須要遵守一些規則,例如:
1、關聯條件必須存在,且盡量唯一確定一行數據。
2、避免使用笛卡爾積,即盡量避免使用多個左連接,調整查詢順序,使查詢結果較小。
3、盡量使用內連接,防止結果集中存在大量null值。
八、資料庫兩個表關聯查詢語句
針對不同的資料庫,兩個表關聯查詢的語句也有一些差異。
例如,在MySQL中,我們可以使用join關鍵字來進行關聯查詢,如下:
SELECT student.name, score.score FROM student INNER JOIN score ON student.id=score.student
而在Oracle資料庫中,則使用「+」運算符進行關聯查詢,如下:
SELECT student.name, score.score FROM student, score WHERE student.id=score.student(+)
在SQL Server中,可以使用left/right join關鍵字進行關聯查詢,如下:
SELECT student.name, score.score FROM student LEFT JOIN score ON student.id=score.student
以上是關於SQL兩個表關聯查詢的詳細介紹,希望對大家有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/193125.html