Error for INNER JOIN with GROUP BY and WHERE claus

2019-08-27 21:39发布

I've got these tables

student:

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

and,

email:

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

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

I don't have much experience in SQL.

1条回答
放荡不羁爱自由
2楼-- · 2019-08-27 22:03

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.

查看更多
登录 后发表回答