I have a chat_member table which includes the foreign keys user_id and channel_id, only one of which will ever have a value. I want to select from this table and inner join on the foreign_key where the username field of the foreign key record matches a particular value. What I have right now is:
SELECT CHAT_MEMBERS.*
FROM CHAT_MEMBERS
INNER JOIN USERS ON USERS.ID = CHAT_MEMBERS.USER_ID
INNER JOIN CHATS ON CHATS.ID = CHAT_MEMBERS.CHANNEL_ID
WHERE ((CHAT_MEMBERS.CHAT_ID = '-1001511551513')
AND (USERS.USERNAME = 'time4code'))
OR ((CHAT_MEMBERS.CHAT_ID = '-1001511551513')
AND (CHATS.USERNAME = 'time4code'))
LIMIT 1
which doesn't work, I assume because the inner join fails for one of the records. If I remove the first inner join and the first part of the WHERE clause it works.
One question, kind off the topic, is this query correct in syntax?
Обсуждают сегодня