sql server - How to use join in SQL -


i beginner sql query, can't quite understand how join works

here sql query

select      cast (t_users.userid int) userid,     t_users.firstname,      t_users.lastname,      t_users.email,     t_users_in_project.projectid,     t_teachers.teacherid,     t_teachers.firstname + ' ' + t_teachers.lastname teachername,     t_teachers.email teacheremail t_users left join t_users_in_project on t_users_in_project.userid = t_users.userid inner join t_projects on t_projects.projectid = t_users_in_project.projectid left join t_teachers on t_teachers.teacherid = t_projects.teacherid t_users_in_project.projectid not null order t_users.userid asc 

and got results

+--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+ | userid | firstname | lastname |    email    | projectid | teacherid | teachername | teacheremail | +--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+ |    284 | person    |        | persona.com |      1951 |        94 | teacher   | teachera.com | |    284 | person    |        | persona.com |       245 |        68 | teacher b   | teacherb.com | |    284 | person    |        | persona.com |       139 |        41 | teacher c   | teacherc.com | |    284 | person    |        | persona.com |       176 |        41 | teacher c   | teacherc.com | +--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+ 

meanwhile want like

+--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+ | userid | firstname | lastname |    email    | projectid | teacherid | teachername | teacheremail | +--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+ |    284 | person    |        | persona.com |      1951 |        94 | teacher   | teachera.com | |    284 | person    |        | persona.com |       245 |        68 | teacher b   | teacherb.com | |    284 | person    |        | persona.com |       176 |        41 | teacher c   | teacherc.com | +--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+ 

basically, want user distinct teacher instead of distinct project. how can retrieve sql?

the database structure like

user      userid, firstname, lastname email users_in_project      userid, projectid project       projectid, teacherid teacher      teacherid, firstname, lastname, email 

thanks

try use max() , group by this

select cast(t_users.userid int) userid, t_users.firstname,        t_users.lastname, t_users.email, max(t_users_in_project.projectid) projectid,        t_teachers.affiliateid,        t_teachers.firstname + ' ' + t_teachers.lastname teachername,        t_teachers.email teacheremail   t_users   left join t_users_in_project on t_users_in_project.userid = t_users.userid  inner join t_projects on t_projects.projectid = t_users_in_project.projectid   left join t_teachers on t_teachers.teacherid = t_projects.teacherid  t_users_in_project.projectid not null  group t_users.userid, t_users.firstname, t_users.lastname, t_users.email,           t_teachers.affiliateid, t_teachers.firstname, t_teachers.lastname,           t_teachers.email  order t_users.userid asc 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -