Question

I have two tables.

Users - Has 2 foreign keys reg_ip and last_ip which both reference the second table column id.

users

+--------+---------+
| reg_ip | last_ip |
+--------+---------+
|      1 |       2 |
+--------+---------+

ips

+----+---------+
| id | user_ip |
+----+---------+
|  1 | 1.2.3.4 |
|  2 | 2.3.4.5 |
+----+---------+

I have been trying to query in such a way that it will return 1.2.3.4 and 2.3.4.5 in one result but I have not been successful. I would be appreciative for a working answer.

Thank you.

Was it helpful?

Solution

Try this:

SELECT GROUP_CONCAT(DISTINCT i.user_ip)
FROM ips i 
INNER JOIN users u ON i.id IN (u.reg_ip, u.last_ip)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top