Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Selecting multiple foreign keys

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.

like image 794
Steve Avatar asked Dec 31 '25 03:12

Steve


1 Answers

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)
like image 129
Saharsh Shah Avatar answered Jan 02 '26 19:01

Saharsh Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!