Im coming back to needing to use SQL
again after not using it for years, and im running into an issue when trying to do something I guess would be considered a join. Heres what I got. I got a username which I need to reference back to a user_id
, which is a simple
SELECT chat_user_id FROM chat_users WHERE chat_username = 'A1A1'
That works fine, but now I need to pass the chat_user_id
to another query to get the chats related to that user_id
. I know I have done this before, but havent done it in a long time. Any hel开发者_高级运维p would be greatly appreciated.
SELECT whatever
FROM chats c
INNER JOIN chat_users cu ON c.chat_user_id = cu.chat_user_id
WHERE ch.chat_username = 'A1A1'
I am not sure specifically what you are asking without knowing the schemas for each. From the vague description you gave, you could do a subquery.
Example:
SELECT somethingElse FROM someOtherTable WHERE chat_uer_id IN (SELECT chat_user_id FROM chat_users WHERE chat_username = 'A1A1');
While subqueries are not the most efficient thing in the world, that will work for what you are doing (If the subquery does not return multiple rows, then you can use "=" instead of "IN").
If you give me more information on the schema and what you are trying to do, I can help you with a more advanced JOIN.
SELECT chat_username, chat.*
FROM chat_users
JOIN chat on chat_users.chat_user_id = chat.chat_user_id
WHERE chat_username = 'A1A1'
hey you can do something like this:
SELECT chat_username, chat.*
FROM chat_users
JOIN chat on chat_users.chat_user_id = chat.chat_user_id
WHERE chat_username = 'A1';
精彩评论