2010-01-24 17 views
5

Ho 4 query diverse e ognuna di esse restituisce un set univoco di risultati. Ho bisogno di combinare i risultati della query con una singola query.Combina più risultati di query in MySQL (per colonna)

mie query di esempio sono:

1. select cls.* from (calls as cls inner join calls_users as clsusr on cls.id=clsusr.call_id) inner join users as usr on usr.id=cls.assigned_user_id where cls.assigned_user_id='seed_max_id' 

2. select mtn.* from (meetings as mtn inner join meetings_users as mtnusr on mtn.id=mtnusr.meeting_id) inner join users as usr on usr.id=mtn.assigned_user_id where mtn.assigned_user_id='seed_max_id' 

3. select tsk.* from tasks as tsk inner join users as usr on usr.id=tsk.assigned_user_id where tsk.assigned_user_id='seed_max_id' 

4. select nts.* from (notes as nts inner join accounts as acnts on acnts.id=nts.parent_id) inner join users as usr on usr.id=acnts.assigned_user_id where acnts.assigned_user_id='seed_max_id' 

ho provato la seguente modo, ma non ha funzionato

Combine: SELECT tbl1.*, tbl2.* 
from (select cls.* from (calls as cls inner join calls_users as clsusr on cls.id=clsusr.call_id) inner join users as usr on usr.id=cls.assigned_user_id where cls.assigned_user_id='seed_max_id') as tbl1 
left outer join 
(select mtn.* from (meetings as mtn inner join meetings_users as mtnusr on mtn.id=mtnusr.meeting_id) inner join users as usr on usr.id=mtn.assigned_user_id where mtn.assigned_user_id='seed_max_id') as tbl2 
using(assigned_user_id) 

Ho provato anche a destra outer join e l'altro interno si unisce Io sono veramente bloccato , se qualcuno conosce la soluzione, per favore aiutatemi. Ho bisogno del risultato simile come How can I join two tables with different number of rows in MySQL?.

Esempio

dati:

da Query 1:

+-------------------------------------------+------------------+- 
| Call Name         | Call Description | 
+-------------------------------------------+------------------+- 
| Discuss Review Process     | NULL    | 
| Get More information on the proposed deal | NULL    | 
| Left a message       | NULL    | 
| Discuss Review Process     | NULL    | 
+-------------------------------------------+------------------+ 

da Query 2:

+-----------------------+----------------------------------------------------------- 
| Meeting Name   | Meeting Description 
+-----------------------+----------------------------------------------------------- 
| Review needs   | Meeting to discuss project plan and hash out the details o 
| Initial discussion | Meeting to discuss project plan and hash out the details o 
| Demo     | Meeting to discuss project plan and hash out the details o 
| Discuss pricing  | Meeting to discuss project plan and hash out the details o 
| Review needs   | Meeting to discuss project plan and hash out the details o 
+-----------------------+----------------------------------------------------------- 

ho bisogno di combinare le colonne come il seguente:

+-------------------------------------------+------------------+-------------------+-------------------+ 
| Call Name         | Call Description |Meeting Name  |Meeting Description| 
+-------------------------------------------+------------------+-------------------+-------------------+ 
| Discuss Review Process     | NULL    |Review needs  |Meeting to discuss | 
| Get More information on the proposed deal | NULL    |Initial discussion |Meeting to discuss | 
| Left a message       | NULL    |Demo    |Meeting to discuss | 
| NULL         | NULL    |Discuss pricing |Meeting to discuss | 
| NULL          | NULL    |Review needs  |Meeting to discuss | 
+-------------------------------------------+------------------+-------------------+-------------------+ 
+0

È necessario descrivere come appaiono i tavoli, come dovrebbe apparire il set di risultati e come combinare i dati. – cletus

+0

Ho bisogno di sapere, c'è un modo per farlo? – Imrul

risposta

5

Il meglio che puoi fare è a UNION o UNION ALL ma ciò richiede che abbiano lo stesso tipo e numero di colonne. Ad esempio:

SELECT 'Customer' AS type, id, name FROM customer 
UNION ALL 
SELECT 'Supplier', id, name FROM supplier 
UNION ALL 
SELECT 'Employee', id, full_name FROM employee 

I nomi delle colonne non devono corrispondere. Gli alias della prima parte verranno utilizzati per il resto.

sarò anche aggiungere che, invece di:

select cls.* from (calls as cls inner join calls_users as clsusr on cls.id=clsusr.call_id) inner join users as usr on usr.id=cls.assigned_user_id where cls.assigned_user_id='seed_max_id' 

è necessario rimuovere il subquery inutile e solo fare:

SELECT c.* 
FROM calls c 
JOIN calls_users cu ONc.id = cu.call_id 
WHERE c.assigned_user_id = 'seed_max_id' 

Non c'è bisogno per la complessità extra e quanto sopra è eminentemente più leggibile.

+0

Questo non può essere fatto in UNION, perché ho bisogno di combinare le colonne delle query. – Imrul

0

Presumo che si desidera che l'esempio restituisca una singola riga che combina le voci corrispondenti di tutte queste tabelle. Prova questo e dicci se ha funzionato:

select * from users as usr 
left outer join (calls as cls 
    inner join calls_users as clsusr 
    on cls.id = clsusr.call_id) 
on usr.id = cls.assigned_user_id 

left outer join (meetings as mtn 
    inner join meetings_users as mtnusr 
    on mtn.id = mtnusr.meeting_id) 
on usr.id = mtn.assigned_user_id 

left outer join tasks as tsk 
on usr.id = tsk.assigned_user_id 

left outer join (notes as nts 
    inner join accounts as acnts 
    on acnts.id=nts.parent_id) 
on usr.id = acnts.assigned_user_id 

where user.id = 'seed_max_id' 
Problemi correlati