Group by query behaviour in mysql -


table in mysql contains below records:

    +----------+----------+-----------+---------+-------+      | personid | lastname | firstname | address | city  |      +----------+----------+-----------+---------+-------+      |        1 | kumar    | amit      | sec-22  | noida |      |        2 | kapoor   | x         | sec-24  | noida |      |        3 | kapoor   | y         | sec-22  | delhi |      |        4 | kapoor   | z         | sec-25  | delhi |      |        5 | kapoor   | w         | sec-25  | gzb   |      +----------+----------+-----------+---------+-------+ 

when run below query:

    select city, firstname persons group city; 

below result shown:

+-------+-----------+ | city  | firstname | +-------+-----------+ | delhi | y         | | gzb   | w         | | noida | amit      | +-------+-----------+ 

i don't understand why firstnames each city not shown in table, there 2 records present noida , delhi each , 1 gzb

please me understanding behaviour.

@amitsh

generally, group used in conjunction aggregate functions (to aggregate data).

assuming firstname "character" field, hence interpreter default grouping column firstname first() value.

you expected output, if suppose, there salary column each individual , want display salaries earned in city. (just example)

you may refer following links understand application of group by.

it if let know expectation query.

reference:

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

http://www.tutorialspoint.com/sql/sql-group-by.htm


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -