Ciao ragazzi sto cercando di fare una query che mi fa impazzire..Vi descrivo le tabelle solo con i campi più significativi che servono...
tabella "utenti"
id
username
tabella "follow_me" (in questa tabella io posso seguire una persona come lui può seguire me e quindi si hanno 2 righe distinte)
id_follow_me
from_utenteid (id dell'utente che vuole seguire un altro)
to_utenteid (id dell'utente seguito)
tabella "amicizie" (in questa tabella ci sarà 1 sola riga perchè io sono amico suo come lui è mio)
id_amicizia
from_friend (id dell'utente che ha richiesto inizialmente l'amicizia)
to_friend (id dell'utente che inizialmente ha accettato l'amicizia)
tabella "blocked_user" (da questa tabella devo vedere se sono stato bloccato)
id_blocked
chi_blocca (id dell'utente che ha fatto partire il blocco)
utente_bloccato (id dell'utente che ha ricevuto il blocco)
ora io ho delle query singole su ogni tabella che funzionano bene e che mi estraggono l'username dell'utente ma io vorrei estrarre l'username di ogni utente con cui ho una relazione in queste tabelle e non riesco a farlo...Anche perchè l'username andrebbe estratto una volta sola..
Ora posto le query singole che ho fatto e vediamo poi come fare per raggruppare tutto..
Codice PHP:
(presupposto che l'id dell'utente che esegue le query sia 311)
Estrarre l'amicizia
select a.*,u.username as nome from amicizie as a
inner join utenti as u on a.to_friend= u.id
where from_friend=311 and request_status=1 and friend_blocker=1
union
select a.*,u.username from amicizie as a
inner join utenti as u on from_friend = u.id
where to_friend=311 and request_status=1 and friend_blocker=1
order by nome
estrarre username degli utenti che io seguo
select f.*,u.username as nome from follow_me as f
inner join utenti as u on f.from_utenteid= u.id
WHERE to_utenteid=311 AND follow_blocker=1
order by nome
estrarre username degli utenti da cui sono seguito
select f.*,u.username as nome from follow_me as f
inner join utenti as u on f.to_utenteid= u.id
WHERE from_utenteid=311 AND follow_blocker=1
order by nome
estrarre username di chi mi ha bloccato
select b.*,u.username as nome from blocked_user as b
inner join utenti as u on b.chi_blocca= u.id
WHERE utente_bloccato=311
order by nome
Ora come faccio a fare una query che mi raggruppi tutte queste estraendomi il nome 1 volta sola?