简体   繁体   English

使用where子句从另一个表中选择数据

[英]Select data from another table with where clause

What's wrong with this query, im selecting data from 3 different tables here. 这个查询出了什么问题,我在这里从3个不同的表中选择数据。 First title of exam from "class_exams" table , second selecting sum of total marks from "results" table. “ class_exams”表中的第一个考试标题,第二次从“ results”表中选择总分数。 Query works fine without where clause. 没有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';

Error comes 错误来了

Unknown column 'exam_title' in 'where clause'

Consider using Join 考虑使用Join

If I understand the table schema , it should be like this : 如果我了解表架构,应该是这样的:

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';

I know this is an old post, but I like to fill the answers where old posts end to prevent dead end posting. 我知道这是旧帖子,但是我想在旧帖子结束处填写答案,以防止出现死胡同。 It looks like the table name is not being called out in the From clause 看起来表名未在From子句中调出

See this link http://www.techonthenet.com/mysql/where.php and look at the example - Joining Tables. 请参见此链接http://www.techonthenet.com/mysql/where.php并查看示例-联接表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM