我有 3 張桌子:
member桌子 :
|--------|---------------------|------------------|
| id | f_name | l_name |
|--------|---------------------|------------------|
| 1 | matt | jim |
| 2 | david | joyce |
|--------|---------------------|------------------|
mentor桌子 :
|--------|---------------------|---------------|
| id | fname | lname |
|--------|---------------------|---------------|
| 1 | mack | pet |
| 2 | larry | oley |
| 3 | roza | masti |
|--------|---------------------|---------------|
class桌子 :
|--------|-----------|----------|
| id | classes | Capacity |
|--------|-----------|----------|
| 1 | Math | 50 |
| 2 | chemistry | 40 |
| 3 | Physics | 60 |
|--------|-----------|----------|
我使用這個 sql 代碼來處理網格中的資料:
select mentor.id,mentor.fname ' ' mentor.lname, class.classes, class.capacity, from mentor, class where mentor.id = class.id
現在我想像這樣在網格中顯示資料來計算類成員的數量
|--------|--------|----------|----------|--------------------|
|mentorid| mentor | classes | Capacity |num of member class |
|--------|--------|----------|----------|--------------------|
| 1 | mack | Math | 50 | 10 |
| 2 | larry |chemistry | 40 | 12 |
| 3 | roza |Physics | 60 | 7 |
|--------|--------|----------|----------|--------------------|
數字 10、12 和 7 是從以下查詢中獲得的:Select count(*) as num of member class from member where class = "Math". 但我不能在上面的查詢中使用這個子查詢來獲得一個結果
問題是如何計算班級成員的數量并將其與其他資訊一起顯示在網格中?
需要注意的是access是作為資料庫使用的
uj5u.com熱心網友回復:
正如我所說,您需要使用所需資料加入子查詢,并始終使用 JOINS
select
mentor.id
,mentor.fname ' ' mentor.lname
, class.class
, class.capacity
, num_mem.[num of member class]
from mentor INNER JOIN class ON mentor.id = class.id
INNER JOIN (Select class,count(*) as [num of member class] from member GROUP BY class) num_mem ON class.class = num_mem.class
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/490732.html
