Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL JOIN QUERY with MULTIPLE WHERE CLAUSE

Tags:

join

mysql

I need some help with MySQL query. Hope someone can help me 'cause Im having a hard time.

Here's my query so far:

"SELECT * 
FROM `users` 
JOIN `m_table1` ON users.user_id = m_table1.user_id
JOIN `m_table2` ON users.user_id = m_table2.user_id
JOIN `m_table3` ON users.user_id = m_table3.user_id
WHERE users.user_id=3"

I want to add month and year on WHERE clause, but month and year column can be found on tables m_table1, m_table2, m_table3.

Thanks!

like image 867
ESL Avatar asked Jun 21 '26 23:06

ESL


1 Answers

Here is a visual, tweak any which way. Uses table aliases too for less typing:

SELECT u.* 
FROM `users`  u
JOIN `m_table1` t1 ON u.user_id = t1.user_id
JOIN `m_table2` t2 ON u.user_id = t2.user_id and t2.year=1942
JOIN `m_table3` t3 ON u.user_id = t3.user_id and t3.year=1942 and t3.month=7
WHERE u.user_id=3
like image 54
Drew Avatar answered Jun 23 '26 13:06

Drew