2
Answers

Join 3 tables using their ids

Hi, I need a sql query which retrieves information from different tables and describe them in a datagrid. These are my tables.
tablename: register
-reg_id(PK)
-date
-timebegin
-begin_photo_id
-timeend
-end_photo_id
-pupils_id
tablename: pupils
-pupils_id(PK)
-name
-surname
-mid_name
-group_id
tablename: photos
-photo_id(PK)
-photo
Query meaning: Select date, timebegin, timeend from register; Select photo from photos where photo_id=begin_photo_if and photo_id=end_photo_id which is related to register table; Select concat(name, surname, mid_name) as fullname from pupils on pupils.pupils_id=register.pupils_id. That means instead register.pupils_id must be fullname which is taken from pupils. And last condition is show them where group_id=combobox.selectedvalue.
Result column names should be:
-fullname
-date
-timebegin
-photo
-timeend
-photo
where group_id=combobox_value
So I need some help, please.
Answers (2)
0
Tapan Patel

Tapan Patel

NA 8.1k 100.9k 7y
Not exact code, but try something like this.
 
  1. string Query = @"  
  2. Select concat(name, surname, mid_name) [fullname],  
  3.        date,  
  4.        timebegin,  
  5.        PStart.photo,  
  6.        timeend,  
  7.        PEnd.photo  
  8. from pupils   
  9. inner join register on register.pupils_id = pupils.pupils_id  
  10. left join photos PStart with (nolock) on PStart.photo_id=begin_photo_id   
  11. left join photos PEnd with (nolock) PEnd.onphoto_id=end_photo_id  
  12. where group_id = @group_id";  
  13.   
  14. SqlCommand cmd= new SqlCommand(query,yourConnection);  
  15. SqlParameter param = new SqlParameter("group_id",SqlDbType.Varchar);  
  16. param.Value = combobox.selectedvalue.tostring();  
  17. cmd.Parameters.Add(param);  
  18. var result = cmd.ExecuteReader();  
 
Accepted
0
abdujalil  chuliev

abdujalil chuliev

NA 181 13.6k 7y
Thanks