Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it L'avatar di strae
    Registrato dal
    Apr 2008
    Messaggi
    407

    [MYSQL] Query su tabelle diverse, 'unire' i risultati..

    ciao ragazzi, ho una struttura del tipo

    Codice PHP:
    tab_a 
    |id|colore|tipo
    Nero W
    Nero E
    Blu Q

    tab_b
    |id|colore|tipo
    Blu W
    Verde Q
    Blu E

    tab_c 
    |id|colore|tipo
    Nero Q
    Verde W
    Blu 

    ora, io avrei bisogno di formulare una query che mi unisca i risultati, prendendo ad esempio tutte le righe delle 3 tabelle che hano tipo W.

    ho provato con

    Codice PHP:
    SELECT
        tab_a
    .id AS idA,
        
    tab_a.num AS numA,
        
    tab_a.colore AS coloreA,
        
        
    tab_b.id AS idB,
        
    tab_b.num AS numB,
        
    tab_b.colore AS coloreB,
        
        
    tab_c.id AS idC,
        
    tab_c.num AS numC,
        
    tab_c.colore AS coloreC 
        
    FROM
        tab_a
            INNER JOIN
        tab_b ON tab_a
    .tipo tab_b.tipo
            INNER JOIN
        tab_c ON tab_c
    .tipo tab_a.tipo
    WHERE
        tab_a
    .tipo 'W' 
    che funzionerebbe anche, ma mi restituisce questo array:
    Codice PHP:
    Array ( [0] => [idA] => [1] => [numA] => [2] => Nero [coloreA] => Nero [3] => [idB] => [4] => [numB] => [5] => Blu [coloreB] => Blu [6] => [idC] => [7] => [numC] => [8] => Verde [coloreC] => Verde 
    io invece avrei bisogno che l'array che mi restituisce fosse qualcosa del tipo
    Codice PHP:
    Array ( [0] => tab_a [id] => [colore] => Nero [tipo] => W)
    Array ( [
    0] => tab_b [id] => [colore] => Blu [tipo] => W)
    Array ( [
    0] => tab_c [id] => [colore] => Verde[tipo] => W
    cioè che fosse specificato in un qualche modo, in una delle colonne, la tabella di appartenenza.. e dopo i dati fossero 'singoli', non un'unica riga...

    mi sono spiegato a parole veramente amle, come a mio solito, spero che gli esempi siano chiari..
    You HAVE to assume your visitor is a maniac serial killer, out to destroy your application. And you have to prevent it.
    I can accept failure, everyone fails at something - But I can't accept not trying.

  2. #2
    Utente di HTML.it
    Registrato dal
    Sep 2002
    Messaggi
    221
    se nn hanno una chiave esterna (e mi sembra questo il caso) puoi usare la sql UNION
    ciAo

  3. #3
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,923
    codice:
    select *,'tabella_A' as tabella from tab_a where tipo = 'W'
    union all
    select *,'tabella_B' from tab_b where tipo = 'W'
    union all
    select *,'tabella_C' from tab_c where tipo = 'W'

  4. #4
    Utente di HTML.it L'avatar di strae
    Registrato dal
    Apr 2008
    Messaggi
    407
    Originariamente inviato da nicola75ss
    codice:
    select *,'tabella_A' as tabella from tab_a where tipo = 'W'
    union all
    select *,'tabella_B' from tab_b where tipo = 'W'
    union all
    select *,'tabella_C' from tab_c where tipo = 'W'

    esatto, proprio quello che mi serviva.. grazie a entrambi!
    You HAVE to assume your visitor is a maniac serial killer, out to destroy your application. And you have to prevent it.
    I can accept failure, everyone fails at something - But I can't accept not trying.

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.