Visualizzazione dei risultati da 1 a 2 su 2

Discussione: domanda JOINS

  1. #1

    domanda JOINS

    Ciao, ho una domanda su MySQL e una query.

    In origine avevo tre tabelle:

    users, tapes, teams

    Al fine di collegare gli utenti (users) al team cui appartengono avevo creato un campo team_id dentro la tabella users, di modo che, per ottenere i miei dati (cioè, i dati di cui avevo bisogno) facevo un cosa del genere:

    SELECT t.tape_title, t.tape_desc, u.username, t2.team_name
    FROM tapes AS t
    INNER JOIN users AS u ON t.user_id = u.user_id
    INNER JOIN teams AS t2 ON u.team_id = t2.team_id
    ORDER BY t.tape_title DESC

    Ad un certo punto, però, mi è stato detto che non avrei dovuto alterare la tabella users aggiungendo quel campo custom (team_id). In pratica, la tabella users non può essere toccata, deve rimanere così com'è.

    Ragion per cui, ho messo su una tabella intermedia detta team_members che ha per campi team_members_id (univoco, autoincrement), user_id (collegata a users), team_id (collegato a teams). Ora, usando questo nuovo disegno, vorrei riuscire ad avere gli stessi dati che prima ottenevo tramite il mio banale campo che si trovava nella tabella users.

    Ho provato con una terza JOIN ma senza risultati apprezzabili...
    Come si potrebbe fare?

    Ciao, grazie a tutti!



    Allego le tabelle semplificate:

    users
    user_id | ...

    tapes
    tape_id | user_id | tape_desc | tape_title

    teams
    team_id | team_name

    team_members
    team_member_id | user_id | team_id

  2. #2

    [SOLVED]

    SELECT tt.tape_title, tt.tape_desc, u.username, t.team_name
    FROM tapes AS tt
    INNER JOIN users AS u ON tt.user_id = u.user_id
    INNER JOIN team_members tm ON tm.user_id = u.user_id
    INNER JOIN teams AS t ON tm.team_id = t.team_id
    ORDER BY tt.tape_title DESC

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.