So i have got these two MySQL tables. I am trying to join the two statements to find out which TelNumber the senderNo is associated with:
I have tried this query but it only returns the messages that have a associated attendee, not ones that dont have an associated telNumber registered on the database.
SELECT Attendee.`ownerName`, Attendee.`businessName`,Messages.`messageID`, Messages.`senderNo`, Messages.`messageContent`,Messages.`timestamp`,Messages.`type`,Messages.`viewed`
FROM Messages
INNER JOIN Attendee ON Messages.`senderNo`= Attendee.`telNumber` OR Attendee.emailAddress
The or equals part is because senderNo can also contain and Email address in which the type would be Email
Thanks in advance.
You can't use OR
to compare one column to two columns like that, you need to do both comparisons separately and join them with OR
.
ON Messages.senderNo = Attendee.telNumber OR Messages.senderNo = Attendee.emailAddress
You can also use IN
to compare a value to multiple things:
ON Messages.senderNo IN (Attendee.telNumber, Attendee.emailAddress)
To get messages that don't have a matching attendee, use LEFT JOIN
instead of INNER JOIN
.
The structure of your query is incorrect. After the OR, it should be
Messages.senderNo = Attendee.emailAddress
To get the messages that don't have an associated telNumber, you need to use LEFT OUTER JOIN instead of INNER JOIN