PostgreSQL: Appendix of field to sample request from several tables



  • Database chart (posgresql):

    Student(id, name, last_name, e_mail)
    Teacher(id, name, last_name, e_mail, subject)
    

    It is necessary:

    Delete alllast_name’ and ‘name’ all ‘Teacher’ and ‘Studentwith field ‘type‘student or teacher. Disorder in alphabetical order by ‘last_name’. It should look like this:

    last_name   | type
                | 
    Kankava     | teacher
    Smith       | student
    Sidorov     | teacher
    Petrov      | teacher
    

    I don't know how. SELECT Add the column TYPE and tie her to the table?



  • SELECT * FROM
          (SELECT name, last_name, 'student' AS type FROM Student
            UNION
          SELECT name, last_name, 'teacher' AS type FROM Teacher)
       AS forexempl ORDER BY last_name;
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2