简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM