I am new to SQL. Can someone help to convert this query, so it uses join syntax?
SELECT groups.name,
users.firstname,
users.lastname,
users.ema开发者_Python百科iladdress,
groupmembers.userid,
reminders.groupid,
reminders.ownerid,
reminderdetails.recno,
reminderdetails.cardid,
reminderdetails.message
FROM reminderdetails,
reminders,
groupmembers,
users,
groups
WHERE assocdate = 'Y'
AND ( reminder1 != 99
AND ( Datediff(reminderdate, '2011-09-22') = reminder1 )
OR ( reminder2 != 99
AND Datediff(reminderdate, '2011-09-22') = reminder2 ) )
AND reminders.cardid = reminderdetails.cardid
AND groupmembers.groupid = reminders.groupid
AND groupmembers.sendemail = 'Y'
AND users.recno = groupmembers.userid
AND groups.recno = reminders.groupid
Try this:
SELECT g.name,
uu.firstname,
uu.lastname,
uu.emailaddress,
gp.userid,
rm.groupid,
rm.ownerid,
rd.recno,
rd.cardid,
rd.message
FROM reminderdetails rd
JOIN reminders rm on rm.cardid=rd.cardId
JOIN groupmembers gp on gp.groupid=rm.groupId and gp.sendemail='Y'
JOIN users uu on uu.recNo=gp.userId,
JOIN groups g ON g.recno=rm.groupId
WHERE assocdate = 'Y'
AND ( reminder1 != 99
AND ( Datediff(reminderdate, '2011-09-22') = reminder1 )
OR ( reminder2 != 99
AND Datediff(reminderdate, '2011-09-22') = reminder2 ) )
Bit of a guess without knowing your table structure, but I think this seems about right.
Edit: Just seen the other one; I'd generally prefer to not load up the JOIN with too many clauses, as you end up going hunting all over the place when you want to find out what WHERE clauses are being applied. If possible, I think it's better to keep JOINs containing just the JOIN and put the rest into the WHERE.
I'm sure someone else can come along and shoot me down on that one, but that's my take on it :)
SELECT groups.name,
users.firstname,
users.lastname,
users.emailaddress,
groupmembers.userid,
reminders.groupid,
reminders.ownerid,
reminderdetails.recno,
reminderdetails.cardid,
reminderdetails.message
FROM groups
INNER JOIN groupmembers ON groups.groupid = groupmembers.groupid
INNER JOIN users ON groupmembers.userid = users.userid
INNER JOIN reminders ON users.userid = reminders.ownderid
INNER JOIN reminderdetails ON reminders.cardid = reminderdetails.cardid
WHERE assocdate = 'Y'
AND ( reminder1 != 99
AND ( Datediff(reminderdate, '2011-09-22') = reminder1 )
OR ( reminder2 != 99
AND Datediff(reminderdate, '2011-09-22') = reminder2 ) )
AND groupmembers.sendemail = 'Y'
精彩评论