Question

I've got these three tables in the DB & I want to select the event_name for a specific userID from t1event given that I know the value of ID from t1user. How can I do this in a single select statement. (I am using mysql).

**t1user**
+----+
| ID |
+----+


**t2userEvent**
+---------+----------+
| userID  |  eventID |
+---------+----------+   


**t1event**
+----------+--------------+
| eventID  |   event_name |
+----------+--------------+
Was it helpful?

Solution

Use join:

SELECT t1user.ID, t1event.event_name
FROM t1user
JOIN t2userEvent ON t1user.ID = t2userEvent.userID
JOIN t1event ON t1event.eventID = t2userEvent.eventID
WHERE t1user.ID = :user_id

If you want the users who doesn't have events be listed too, then use LEFT JOIN instead.

OTHER TIPS

You could try this:

SELECT A.event_name FROM t1event A INNER JOIN t2userEvent B  
ON A.eventID = B.eventID WHERE b.userID = ?

If I understand it correctly you have the userID as parameter?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top