Domanda

Ho un tavolo da ping-utenti e gli amici. L'utente con ID 1 ha 4 amici (con ID 2,3,4,13). Voglio ottenere i nomi e cognomi degli utenti 2,3,4 e 13. Posso ad esso con query standard ma sarà rallentare il processo; query per ogni amico. Non ho avuto come e cosa usare di sinistra, giunti, giunti a destra, giunti semplici, AS, on, ...

entrare descrizione dell'immagine qui

È stato utile?

Soluzione

If you want to get the names of the friends for a user perhaps try something like so:

SELECT u.firstname, u.surname 
FROM friend f, user u
WHERE (f.Person1 = 1 OR f.Person2 = 1)

This should get a back a list of names in your user table (providing that is what you have named your friends in the user table) that are friends with userID 1

I'm guessing your friend table contains the foreign key IDs of the friends making up the partnership?

Altri suggerimenti

I am assuming the friends table has a user_id field that corresponds to the id field in the users table.

Select name, surname from users
where id in 
(select user_id from friends where id=1)

This way, you can just plug the user id in the place where '1' is, and you will get all the respective friends. For example, if you want to find the name and surname of all the friends of the user id 5740 use:

Select name, surname from users
where id in 
(select user_id from friends where id=5740)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a dba.stackexchange
scroll top