how to create a view of self join in SQL

Minu :

While trying to create a view of following query, I get stuck with an error. I have a table named student and i want a view named birthday_twins which contain names and dob of birthday twins.

CREATE VIEW birthday_twins AS
SELECT s.student_name,p.student_name,s.date_of_birth 
FROM student s,student p 
WHERE s.student_id <> p.student_id AND s.date_of_birth=p.date_of_birth 
GROUP BY s.date_of_birth;

Error Code : 1060 Duplicate column name 'student_name'

I am completely new to sql. Can anyone help me to solve this?

MaxT :

you need to give aliases to column names so that view can differentiate field names. i.e.:

CREATE VIEW birthday_twins AS 
SELECT s.student_name name1,p.student_name name2,s.date_of_birth 
FROM student s,student p 
WHERE s.student_id <> p.student_id 
AND s.date_of_birth=p.date_of_birth 
GROUP BY s.date_of_birth;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=5601&siteId=1