2013-06-03 7 views
5

Ho 4 tabelle ACCOUNTS_TABLE, LINKS_TABLE, GROUPS_TABLE, KEYS_TABLE enter image description hereCome scrivere JOIN query per 4 tavoli nella condizione di sotto

enter image description here

enter image description here

enter image description here

ho bisogno di ottieni all accounts details che è di tipo acct_ con count of Links, groups& keywords. Ho provato questa query ma dà tutto count as 0

SELECT 
    acc.acct_id, acc.acct_type, count(link.id) as link_count, link.account, 
    groups.camp_id, count(groups.id) as group_count, count(keyword.key_id) as key_count 

FROM ".ACCOUNTS_TABLE." as acc 
    LEFT JOIN ".LINKS_TABLE." as link ON link.account=acc.acct_id AND acct_type='xx' 
    LEFT JOIN ".GROUPS_TABLE." as groups ON groups.camp_id=link.id 
    LEFT JOIN ".KEYS_TABLE." as keyword ON keyword.camp_id=link.id 

GROUP BY acc.acct_id 

mia uscita richiesta dovrebbe essere come questo enter image description here

Uno please help me a slove questo problema

risposta

1

Probabilmente dovrebbe usare COUNT (DISTINCT ....).

SELECT acc.acct_id, COUNT(DISTINCT link.id), COUNT(DISTINCT groups.id), COUNT(DISTINCT keyword.key_id) 
FROM ACCOUNTS_TABLE acc 
LEFT OUTER JOIN LINKS_TABLE link ON link.account = acc.acct_id AND acct_type = 'advertiser' 
LEFT OUTER JOIN GROUPS_TABLE groups ON groups.camp_id = link.id 
LEFT JOIN KEYS_TABLE keyword ON keyword.id = link.id 
WHERE acc.acct_type = 'xx' 
GROUP BY acc.acct_id 

EDIT

modificata per utilizzare le condizioni aggiornati unirsi, ecc: -

SELECT acc.acct_id, acc.acct_type, COUNT(DISTINCT link.id) , COUNT(DISTINCT groups.id) , COUNT(DISTINCT keyword.key_id) 
FROM ACCOUNTS_TABLE acc 
LEFT OUTER JOIN LINKS_TABLE link ON link.account = acc.acct_id 
LEFT OUTER JOIN GROUPS_TABLE groups ON groups.camp_id = link.id 
LEFT JOIN KEYS_TABLE keyword ON keyword.camp_id=link.id 
WHERE acc.acct_type = 'xx' 
GROUP BY acc.acct_id, acc.acct_type 
+0

Ancora il suo conteggio a zero – Juice

+0

quando ometto 'GROUP BY acc.acct_id' fornisce correttamente il conteggio totale. Ma ho bisogno di unire tutti gli account separatamente! – Juice

+0

La cosa che sembra impedirgli di riportare dati utili con i dati di test è che hai specificato un acct_type di "inserzionista" nel join nella tabella dei link. Non ci sono record corrispondenti per questo. La rimozione di questo ottiene alcuni record. Inoltre, hai specificato di unirti alla tabella delle chiavi usando key_id e l'id delle tabelle di collegamento. Vuoi unirti a questi su questi o su camp_id che avrebbe più senso? – Kickstart

0

Si potrebbe provare qualcosa di simile:

SELECT ACC.Id 
     ,(SELECT COUNT (*) FROM Links L WHERE L.AccountId = ACC.Id) AS CountOfLinks 
     ,(SELECT COUNT (*) FROM Groups G WHERE G.AccountId = ACC.Id) AS CountOfGroups 
FROM (SELECT Id FROM Accounts Acc WHERE Acc.Type = 'some type') ACC 
+0

Perché la subquery? –

+1

Le sottoquery correlate a Co sono MOLTO costose. –

+0

@OlivierCoilland: hai ragione, potresti perfettamente fare a meno. – souplex

0
SELECT 
    accounts_table.acct_id, 
    accounts_table.acct_type, 
    COUNT(DISTINCT links_table.id) AS link_count, 
    COUNT(DISTINCT groups_table.id) AS group_count, 
    COUNT(DISTINCT keys_table.key_id) AS key_count 
FROM 
    accounts_table 
LEFT JOIN 
    links_table 
    ON links_table.account = accounts_table.acct_id 
LEFT JOIN 
    groups_table 
    ON groups_table.camp_id = links_table.id 
LEFT JOIN 
    keys_table 
    ON keys_table.camp_id = links_table.id 
WHERE 
    acct_type = 'xx' 
GROUP BY 
    accounts_table.acct_id, 
    accounts_table.acct_type 
ORDER BY 
    link_count DESC, 
    group_count DESC, 
    key_count DESC 

Risposta modificata per far corrispondere la domanda aggiornata: questo dovrebbe fare ciò che hai chiesto.

Questo dovrebbe fare quello che hai chiesto, violino SQL qui - http://www.sqlfiddle.com/#!2/f4b6a/20

+0

Questa è la stessa query che ho fornito. – Juice

+0

SQL aggiornato ora –

0

ho rejigged il codice un po '(vedi sotto) per un paio di motivi:

  1. E' utile (per io comunque) per scrivere le mie affermazioni SELECT sempre in un certo modo - con tutto ciò che non è raggruppato collocato per primo, e idealmente mettendo le cose nello stesso ordine dei miei JOIN e facendo lo stesso nel mio GRUPPO PER
  2. metto tutto ciò che limita my FROM table in WHERE not the JOIN per rendere più chiaro ciò che sto cercando di fare e a anche per semplificare la modifica in seguito.
  3. Mi piace anche assicurarmi che sia ben strutturato per facilitare la scansione dei problemi.

Prendi questa query riorganizzata e leggila per accertarti di ottenere il comportamento che ti aspetti.

PS Non sono sicuro dei nomi delle tabelle e dello stile delle quotazioni: di solito uso i tick indietro (`) e non inserisco mai punti (.) Nei nomi delle tabelle. Se li metti come segnaposti va bene, ma potrebbero causare problemi se fossero reali.

SELECT 
acc.acct_id, 
-- if you don't group by these then you need to remove them as they will just return the first values based on mysql behaviour 
acc.acct_type, 
link.account, 
groups.camp_id, 
-- these counts will only count where an ID is present which seems like what you're after 
count(link.id) as link_count, 
count(groups.id) as group_count, 
count(keyword.key_id) as key_count 
FROM ".ACCOUNTS_TABLE." as acc 
LEFT JOIN ".LINKS_TABLE." as link ON link.account=acc.acct_id 

LEFT JOIN ".GROUPS_TABLE." as groups ON groups.camp_id=link.id 
LEFT JOIN ".KEYS_TABLE." as keyword ON keyword.id=link.id 

WHERE acct_type='advertiser' 

GROUP BY acc.acct_id, 
-- only use these if you intend to group by them 
acc.acct_type, 
link.account, 
groups.camp_id DESC 
0
SELECT acct_type, 
     count(acct_type), 
     count(l.id), 
     count(g.id), 
     count(key_id) 
FROM accounts a 
LEFT JOIN links l ON (l.account = a.acct_id) 
LEFT JOIN groups g ON (g.camp_id = l.id) 
LEFT JOIN keysTable k ON k.group_id = g.id 
GROUP BY acct_type HAVING acct_type = 'xx'; 

SQL Fiddle convalidati: http://www.sqlfiddle.com/#!2/f4b6a/20

+0

Ho bisogno di ottenere tutti i dettagli dell'account conteggi separatamente. Dà solo il conto totale. cioè, acc1 ha 2 link 1 gruppo e 3 parole chiave, acc2 ha 3 link 3 gruppi e 1 parola chiave come saggio – Juice