Pregunta

Currently i have the sql query

var sql = 'SELECT * FROM [epicatest].[User] WHERE longitude != \'null\' AND latitude != \'null\' AND id != \'' + userId + '\'';

This will return: id: surname: firstname: dob: longitude: latitude: email:

I also have another table called [epicatest].[User_Sports] and inside this there are the fields: id: sport_id

before the query above is called, i have an array stored which contains a list of sports id which are associated with you (origin user).

when the query is called, i would like to expand it so that it only selects the people who share at least one common sport with you (i.e they have an (id), VALUES (sport_id) which you also have

heres an example of what im trying to do, imagine origin user has id of 1

id, sport_id

1, sport a

1, sport b

1, sport c

2, sport a

3, sport f

2, sport g

3, sport l

3, sport h

4, sport c

4, sport h

2, sport k

4, sport z

so if i ran a 'query' it would return only the users with id, 2 and 4, as they are interested in sport a and c, the same as me(origin user). This is what i am trying to achieve, how would i modify this:

var sql = 'SELECT * FROM [epicatest].[User] WHERE longitude != \'null\' AND latitude != \'null\' AND id != \'' + userId + '\'';

to do so?

remember i have an array available to me with all my (origin user) sport_ids

Thanks.

¿Fue útil?

Solución

Ok so you are using Azure, so I am using SQL Server syntax. Also, I am assuming that the table User_Sport has a couple of columns called user_id and sport_id.

Here's the query:

SELECT DISTINCT u.* FROM
    [epicatest].[User] u
INNER JOIN
    [epicatest].[User_Sport] p ON u.id = p.user_id
WHERE
    u.longitude != 'null'
AND
    u.longitude != 'null'
AND
    u.id != @user_id
AND
    p.sports_id IN (1, 2, 3) -- this is the list of your sport IDs, comma-seprated
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top