![](/img/trans.png)
[英]Select from table where clause from array from another table
[英]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.