php - Select mySQL data with help of other table
I'm currently working on a message board which only displays data if values in another table are set.
My 2 tables look like this:
tbsubscribers:
-subID (int)
-msg1 (tinyint)
-msg2 (tinyint)
-msg3 (tinyint)
-email (varchar)
tbmessage
-id (int)
-name (varchar)
-mitteilung (varchar)
-sender (varchar)
-datum (datetime)
The actual messages are in the tbmessage table and to check which messages the user wants to see there is the tbsubscribers table.
Now, I want to display the messages. So if a user subscribed tomsg1
andmsg2
(so the value of both are 1) it should select all the data from tbmessage which has the name'msg1'
and'msg2'
.
I know how to work with select in mysql but don't know how I could select multiple tables, and especially in this case I'm kind of confused how I would do this.
Any help would be appreciated
Answer
Solution:
A simple join should work:
I created a sqlfiddle here, which should demonstrate the functionality you're looking for.
I'm going to make some edits to clarify how you should do this, using both the query above, and another query if need be.
There are a few ways you can do this. Way #1:
Way #2: