Choose from a table where the value is common with another table

I have two tables ( visitedand purchased)

visited -> id,product,user_ip
purchased -> id,product,user_ip

which end up having the same value in the column user_ip. Example:

visited ->
1,product1,192.168.1.1
2,product2,192.168.1.1
3,product3,192.168.12.12
4,product4,192.168.12.12

purchased ->
1,product3,192.168.12.12

Can I choose all productsof the ones visitedthat I have user_ipwith purchased? In the above example, I will need to select id3andid4

Thank you in advance!

+5
source share
2 answers

Try the following:

SELECT *
FROM visited
WHERE user_ip IN (SELECT user_ip FROM purchased)

This can be slow if you don't have the correct indexes set in columns user_ip...

+5
source

Try this, write a comment if you need me to edit.

SELECT *
FROM visited
INNER JOIN purchased
ON visited.user_ip=purchased.user_ip
ORDER BY visited.id

, btw ( ).

+1

All Articles