Vise redova iz baze, ispisati u jednom

Pozdrav :slight_smile:

Imam xy redova u bazi, gdje je prvi stupac recimo trenutni key, a drugi stupac neki drugi key, a u drugom redu suprotno. Key je prijavljen korisnik, a drugi key je neki drugi korisnik. Kako da ispišem samo jedan red za jedan ‘drugi key’, gdje su oba keya unesena. Nadam se da se razumijemo. Najbolji primjer je sustav poruka na FB-u. Pošalješ 10 poruka Osobi B, on ima u porukama Osoba A (10)… Znači ne ispiše 10 redova, nego jedan ‘grupirani’. Može i teoretsko objašnjenje.

Hvala unaprijed

ono što tebi treba jest jedna dobra knjiga o bazama.
prouči: ključeve, indexe, relacije, normalne forme.

Select count(*) as count from tablica group by user_id

http://www.w3schools.com/sql/sql_intro.asp

The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer=‘Nilsen’

SELECT COUNT(DISTINCT column_name) FROM table_name
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.

We have the following “Orders” table:
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen

Now we want to count the number of orders from “Customer Nilsen”.

We use the following SQL statement:
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer=‘Nilsen’

The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total:
CustomerNilsen
2

@gorrc: The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer

Group sa count može raditi count na sve odnose korisnik>korisnik , inače mislim da zvijezdica je najbrže kod counta jer ne provjerava vrijednost u columni.

Znaći može dobiti count svih poruka ka korisnicima groupirano prema korisnicima kojima je slao poruke.

OK, primljeno na znanje. :slight_smile:

Merci, COUNT(*) did ic đab. :slight_smile: