簡體   English   中英

使用where子句從另一個表中選擇數據

[英]Select data from another table with where clause

這個查詢出了什么問題,我在這里從3個不同的表中選擇數據。 “ class_exams”表中的第一個考試標題,第二次從“ results”表中選擇總分數。 沒有where子句的查詢工作正常。

SELECT id, exam_date , (
SELECT title
FROM class_exams
WHERE result_heads.exam_id = class_exams.id
) AS exam_title, (
SELECT sum( marks )
FROM results
WHERE result_heads.id = results.head_id
) AS obt_marks

FROM `result_heads` WHERE exam_title = 'test';

錯誤來了

Unknown column 'exam_title' in 'where clause'

考慮使用Join

如果我了解表架構,應該是這樣的:

SELECT result_heads.id, result_heads.exam_date , sum( results.marks )AS obt_marks
FROM results JOIN result_heads
     ON results.exam_id = result_heads.id
GROUP BY result_heads.id, result_heads.exam_date

我認為您需要在Clouse的位置添加表的名稱

WHERE `tbl_name`.`exam_title = 'test';

我知道這是舊帖子,但是我想在舊帖子結束處填寫答案,以防止出現死胡同。 看起來表名未在From子句中調出

請參見此鏈接http://www.techonthenet.com/mysql/where.php並查看示例-聯接表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM