使用GROUP BY和WHERE子句的INNER JOIN的MySQL错误 [英] MySQL error for INNER JOIN with GROUP BY and WHERE clause

查看:101
本文介绍了使用GROUP BY和WHERE子句的INNER JOIN的MySQL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些表格

学生

id  Name
-------------------
1   john
2   carlos
3   zoya
4   arab
5   amir

电子邮件

id      email   student_id
--------------------------
1   a@mail.com      1
2   b@mail.com      2
3   c@mail.com      2
4   d@mail.com      3
5   e@mail.com      4

我使用这个查询,并得到在查询第4行使用sql错误

I'm using this query and it's getting Using sql error on query line 4,

SELECT * FROM student 
INNER JOIN email 
ON student.id = email.student_id 
GROUP BY student.id
WHERE student.id = 2

我没有太多的SQL经验。

I don't have much experience in SQL.

推荐答案

首先, GROUP BY 总是在 WHERE 子句。

其次,如果您使用 GROUP BY 子句,您应该为字段使用聚合函数,不包含在它仍然存在于<$ c> SELECT 子句中,或 GROUP BY 包含在 SELECT

Firstly, GROUP BY always goes after the WHERE clause.
Secondly, if you are using the GROUP BY clause you should either use aggregate functions for the fields, that are not contained in it and still are present in the SELECT clause, or GROUP BY the whole pack of columns contained in the SELECT.

这篇关于使用GROUP BY和WHERE子句的INNER JOIN的MySQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆