简体   繁体   中英

My sql query can't show my desired result

I have following tables:

 comitees: Id, Name

 comitee_member: Id, Cid, Mid, C_Month

 Reference: Id, Mid, Cid, Month, Ref_Number

 user_type: Id, User_Name, Ref_Number

 notifications: Id, Sender_Name, Reciever_Name, Month

I want to send a notification:

Suppose Mr. A has comitee_member.C_Month=9/9/2015 . When Mr. B submits his reference number for reference.Month=9/9/2015 , Mr. A recieves a notification that shows the sender's name (Mr. B), comitees.Name , and the reference.Month when comitee was submitted. This data must be stored in the Notifications table.

I have this so far:

Select us.Id, 
       c.Name, 
       cm.C_Month, 
       u.User_Name AS Receiver, 
       us.User_Name AS Sender 
FROM  user_type u, 
      user_type us 
INNER JOIN reference r ON us.Id = r.Mid
INNER JOIN comitee_member cm ON r.Month = cm.C_Month
INNER JOIN comitees c ON c.Id = r.Cid
WHERE us.Id = 3 (suppose) 

I guess these relationships from your sql statement:

  • Reference.mid --> user_type(id)
  • Reference.cid --> comitees(id)
  • Reference.month --> comitee_member(C_month)

I miss some relationships. If this is right:

  • comitee_member.mid --> user_type.id?

you can get your results with

Select uSender.Id, 
   c.Name, 
   cm.C_Month, 
   uReceiver.User_Name AS Receiver, 
   uSender.User_Name AS Sender 
FROM user_type uSender
JOIN reference r ON uSender.Id = r.Mid
JOIN comitees c ON c.Id = r.Cid
JOIN comitee_member cm ON r.Month = cm.C_Month
JOIN user_type uReceiver ON uReceiver.Id = cm.Mid;

Optional you can add your where clause. Btw, I used the short form of INNER JOIN , just JOIN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM