Jump to content
gigi23

select count mysql

Recommended Posts

Posted

salut

am 2 tabele

tabel1

ID,nume,prenume,varsta

tabel2

ID,comentarii

cum as putea dintr-o singura comanda sa fac un select sa afiseze

nume,prenume,varsta,count(comentarii)[din tabel2]

Posted

probabil vrei ceva de genu:

users - id,nume,prenume,varsta

comm - id,user_id,comentariu

select id as uid,nume,prenume,varsta,(select count( * ) from comm where user_id = uid) from users

  • Downvote 1
Posted

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.varsta

sau

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...