Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [MySql] Query su più tabelle

    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?

  2. #2
    ho ricreato le query con le union

    Codice PHP:

    SELECT utenti
    .username FROM utenti INNER JOIN follow_me ON follow_me.from_utenteid=utenti.id WHERE follow_me.to_utenteid=311
    UNION
    SELECT utenti
    .username FROM utenti INNER JOIN follow_me ON follow_me.to_utenteid=utenti.id WHERE follow_me.from_utenteid=311
    UNION
    SELECT utenti
    .username FROM utenti INNER JOIN amicizie ON amicizie.from_friend=utenti.id WHERE amicizie.to_friend=311
    UNION
    SELECT utenti
    .username FROM utenti INNER JOIN amicizie ON amicizie.to_friend=utenti.id WHERE amicizie.from_friend=311
    UNION
    SELECT utenti
    .username FROM utenti INNER JOIN blocked_user ON blocked_user.chi_blocca=utenti.id WHERE blocked_user.utente_bloccato=311 
    Mi sembra funzioni...anche se non so se è funzionale in questo modo..

  3. #3
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    Credo che le union siano l'unica soluzione al tuo problema.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.