gigi23 Posted April 14, 2011 Report Posted April 14, 2011 salut am 2 tabeletabel1ID,nume,prenume,varstatabel2ID,comentariicum as putea dintr-o singura comanda sa fac un select sa afisezenume,prenume,varsta,count(comentarii)[din tabel2] Quote
Nytro Posted April 14, 2011 Report Posted April 14, 2011 (edited) SELECT nume,prenume,varsta FROM tabel1 UNION SELECT COUNT(*) FROM table2? Edited April 14, 2011 by Nytro Quote
tdxev Posted April 15, 2011 Report Posted April 15, 2011 probabil vrei ceva de genu:users - id,nume,prenume,varstacomm - id,user_id,comentariuselect id as uid,nume,prenume,varsta,(select count( * ) from comm where user_id = uid) from users 1 Quote
gigi23 Posted April 18, 2011 Author Report Posted April 18, 2011 10x tdxev nu doream cu union,mai era o functie ceva cu left join ? Quote
CyberWolf08 Posted April 18, 2011 Report Posted April 18, 2011 ceva de genu cred..SELECT u.nume, u.prenume, u.varsta, count(c.id) FROM comentarii c LEFT JOIN users u ON c.ID = u.ID group by u.id Quote
gigi23 Posted April 18, 2011 Author Report Posted April 18, 2011 da da .. cred c merge mai repede comanda asta Quote
CyberWolf08 Posted April 18, 2011 Report Posted April 18, 2011 vezi ca left join se fute urat daca ai null-uri... ai grija Quote
cristi_89 Posted April 18, 2011 Report Posted April 18, 2011 SELECT nume, prenume, varsta, count( * ) as comentariiFROM tabel1 as t1INNER JOIN tabel2 as t2 ON t1.id = t2.idGROUP BY t1.id Quote
redox Posted April 18, 2011 Report Posted April 18, 2011 Solutia lui txdev ii cea mai buna. In rest la group by trebuie sa pui nume, prenume etc , adica si celeate campuri din select care nu au o functie agregata (suma, medie, count etc).Daca vrei neaparat left join solutia lui cyberwolf ar trebui (dar putin modificata)SELECT u.nume, u.prenume, u.varsta, count( * ) FROM users u LEFT JOIN comentarii c ON u.ID = c.user_ID group by u.nume, u.prenume, u.varstasau varianta 3:SELECT u.nume, u.prenume, u.varsta, isnull(c.Posturi,0) as Posturi FROM users u LEFT JOIN(select user_ID, count( * ) as Posturi from comentarii group by user_ID) c ON u.ID = c.user_ID Varianata 3 sigur merge sub MS-SQL, sub MySQL s-ar putea sa trebuiasca adaptata Quote