2012-01-13 14 views
5

Ho 5 tabelle. Uno primario e 4 aggiuntivi (hanno colonne diverse).MySQL join tabelle in cui il nome della tabella è un campo di un'altra tabella

  1. oggetti
  2. obj_mobiles
  3. obj_tablets
  4. obj_computers

Ecco la struttura della mia tabella principale (oggetti).

ID | tipo | nome | ecc ...

Quindi quello che voglio fare è quello di unire gli oggetti con altri (obj_mobiles, obj_tablets, ...) le tabelle, a seconda del tipo di campo. So che dovrei usare l'SQL dinamico. Ma non posso fare la procedura. Penso che dovrebbe assomigliare a qualcosa del genere.

SELECT objects.type into @tbl FROM objects; 
PREPARE stmnt FROM "SELECT * FROM objects AS object LEFT JOIN @tbl AS info ON object.id = info.obj_id"; 
EXECUTE stmnt; 
DEALLOCATE PREPARE stmnt; 

Aslo pseudo-codice

SELECT * FROM objects LEFT JOIN [objects.type] ON ... 

Can procedura di post qualcuno? Inoltre voglio avere tutte le righe non solo 1 riga. Grazie.

risposta

7

Se si desidera che tutte le righe (output di massa) e non una riga alla volta, il sotto dovrebbe essere veloce e anche l'output per tutte le righe conterrà tutte le colonne.

Consente di considerare di seguito i campi delle tabelle. obj_mobiles - ID | M1 | M2 obj_tablets - ID | T1 | T2 obj_computers - ID | C1 | Oggetti C2 - ID | tipo | nome | ecc.,

Select objects.*, typestable.* 
from (
    select ID as oID, "mobile" as otype, M1, M2, NULL T1, NULL T2, NULL C1, NULL C2 from obj_mobiles 
    union all 
    select ID as oID, "tablet" as otype, NULL, NULL, T1, T2, NULL, NULL from obj_tablets 
    union all 
    select ID as oID, "computer" as otype, NULL, NULL, NULL, NULL, C1, C2 from obj_computers) as typestable 
     left join objects on typestable.oID = objects.ID and typestable.otype = objects.type; 

+------+--------------------+----------+------+----------+------+------+------+------+------+------+ 
| ID | name    | type  | ID | type  | M1 | M2 | T1 | T2 | C1 | C2 | 
+------+--------------------+----------+------+----------+------+------+------+------+------+------+ 
| 1 | Samsung Galaxy s2 | mobile | 1 | mobile | 1 | Thin | NULL | NULL | NULL | NULL | 
| 2 | Samsung Galaxy Tab | tablet | 2 | tablet | NULL | NULL | 0.98 | 10 | NULL | NULL | 
| 3 | Dell Inspiron  | computer | 3 | computer | NULL | NULL | NULL | NULL | 4.98 | 1000 | 
+------+--------------------+----------+------+----------+------+------+------+------+------+------+ 

La tabella viene creata come di seguito.

mysql> create table objects (ID int, name varchar(50), type varchar (15)); 
Query OK, 0 rows affected (0.05 sec) 
mysql> insert into objects values (1, "Samsung Galaxy s2", "mobile"), (2, "Samsung Galaxy Tab", "tablet"), (3, "Dell Inspiron", "computer"); 
Query OK, 3 rows affected (0.00 sec) 
Records: 3 Duplicates: 0 Warnings: 0 


mysql> create table obj_mobiles (ID int, M1 int, M2 varchar(10)); 
Query OK, 0 rows affected (0.03 sec) 
mysql> insert into obj_mobiles values (1, 0.98, "Thin"); 
Query OK, 1 row affected (0.00 sec) 


mysql> create table obj_tablets (ID int, T1 float, T2 int(10)); 
Query OK, 0 rows affected (0.03 sec) 
mysql> insert into obj_tablets values (2, 0.98, 10); 
Query OK, 1 row affected (0.00 sec) 


mysql> create table obj_computers (ID int, C1 float, C2 int(10)); 
Query OK, 0 rows affected (0.03 sec) 
insert into obj_computers values (3, 4.98, 1000); 

anche per confermare i tipi di dati delle colonne sono stessi delle colonne originali, il risultato viene memorizzato in una tabella e tipi di dati vengono controllati seguito.

create table temp_result as 
Select objects.*, typestable.* 
from (
    select ID as oID, "mobile" as otype, M1, M2, NULL T1, NULL T2, NULL C1, NULL C2 from obj_mobiles 
    union all 
    select ID as oID, "tablet" as otype, NULL, NULL, T1, T2, NULL, NULL from obj_tablets 
    union all 
    select ID as oID, "computer" as otype, NULL, NULL, NULL, NULL, C1, C2 from obj_computers) as typestable 
     left join objects on typestable.oID = objects.ID and typestable.otype = objects.type; 

mysql> desc temp_result; 
+-------+-------------+------+-----+---------+-------+ 
| Field | Type  | Null | Key | Default | Extra | 
+-------+-------------+------+-----+---------+-------+ 
| ID | int(11)  | YES |  | NULL |  | 
| name | varchar(50) | YES |  | NULL |  | 
| type | varchar(15) | YES |  | NULL |  | 
| oID | int(11)  | YES |  | NULL |  | 
| otype | varchar(8) | NO |  |   |  | 
| M1 | int(11)  | YES |  | NULL |  | 
| M2 | varchar(10) | YES |  | NULL |  | 
| T1 | float  | YES |  | NULL |  | 
| T2 | int(11)  | YES |  | NULL |  | 
| C1 | float  | YES |  | NULL |  | 
| C2 | int(11)  | YES |  | NULL |  | 
+-------+-------------+------+-----+---------+-------+ 
11 rows in set (0.00 sec) 
+0

Questo sarà più veloce della soluzione di Devart –

+0

Grazie. Questo è quello che ho cercato. – Cyberon

0

UTILIZZARE CONCAT per creare la vostra dichiarazione preparatoria. es.

@sql = CONCAT('SELECT * FROM objects AS object LEFT JOIN ', @tbl, ' AS info ON object.id = info.obj_id'); 
PREPARE stmnt FROM @sql; 
... 
3

Se objects è una tabella padre vuol dire che objects. ID è un oggetto unico. Destra? Tutti gli altri oggetti (cellulari, tablet, computer) sono secondari per oggetto e i dispositivi mobili e i tablet non possono avere lo stesso ID. Se è così, è sufficiente utilizzare questo semplice query -

SELECT * FROM objects o 
    LEFT JOIN obj_mobiles m 
    ON o.id = m.ID 
    LEFT JOIN obj_tablets t 
    ON o.id = t.ID 
    LEFT JOIN obj_computers c 
    ON o.id = c.ID 

Aggiungi clausola WHERE per filtrare i tipi, ad es .: DOVE o.type = 'mobile'.

+0

Controllare la mia soluzione per una migliore query di prestazioni, con join minori. –

Problemi correlati