1
I need to do the following. So-and-so has several car models. I want to list other users who also have 3 specific car models that so-and-so has.
What I did:
$iduser = id do fulano
$mod1 = modelo de carro 1 de fulano
$mod2 = modelo de carro 2 de fulano
$mod3 = modelo de carro 3 de fulano
I have a table (user
) for users and a table (user_mod
) for the templates that each user has. So I need to find other users that are in the user_mod table that have the same idmodelo
from the so-and-so for each of the 3 so-and-so car models.
SELECT a.*
FROM users a
WHERE a.IDUser!=$iduser AND ('$idmod1,$idmod2,$idmod3')
IN (Select d.IDModelo From user_mod d Where a.IDUser=d.IDUser)
I tried that, but it didn’t work. He always considers only the $idmod1
. That is, if the user has this model inside his SELECT
, it is already listed. I wanted to list only users who necessarily had the 3 models $idmod1,$idmod2,$idmod3
within the outcome of SELECT
subquery. Does anyone know if there are any commands for this to work? I am using Mysql database.
Thank you very much!
You are programming with PHP?
– Victor Stafusa
Yes, but I would need this selection to be done within the SQL pq itself if I first list all users to then loop in PHP and identify those who have the same 3 car models, will run too loop pq are many users (I don’t know if that’s what you were going to propose).
– Peter
One solution I found was to do 3 subquery, each one for a template. ('$idmod1) IN (Select d.IDModelo From user_mod d Where a.IDUser=d.IDUser) AND ($idmod2) IN (Select d.IDModelo From user_mod d Where a.IDUser=d.IDUser) AND ('$idmod3') IN (Select d.IDModelo From user_mod d Where a.IDUser=d.IDUser).... But this will not solve when there are 10 car models at the same time. It will be 10 subquery and a lot of other conditions Where together. It will require a lot of database.
– Peter