Forum Moderators: mack

Message Too Old, No Replies

query problem

         

hswaseer

1:22 pm on Jan 8, 2005 (gmt 0)

10+ Year Member



Hi,

My Mysql database contains three fields

id-game-player

1-football-tim
2-football-jim
3-baseball-kim
4-baseball-gim
5-basketball-ali
6-basketball-kali

how should i write a query so that it should print the players coming under particular game i.e.

Football
Tim
Jim

Baseball
Kim
Gim

basketball
ali
kali

Thanks

HS

mcibor

2:18 pm on Jan 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT player, game FROM table GROUP BY game;

However it will return such:

Tim Football
Jim Football

Kim Baseball
Gim Baseball

ali basketball
kali basketball

However that you can manually correct, or leave it as such

hswaseer

7:20 am on Jan 10, 2005 (gmt 0)

10+ Year Member



Hi

Well, the code provided by you is not working properly ... It is returning as

Football
Tim

Baseball
Kim

basketball
ali

The name of all the players are not coming under particular game. Only one name is displaying in each game. Can u elaborate more on this.

Thanks

HS

mcibor

9:37 pm on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



True...

This is corrected question:

SELECT player, game FROM table GROUP BY game, player;

It will even return twice players that are playing football and basketball, however it will ommit records where player and game already appeared:

id-game-player

1-football-tim
2-football-jim
3-baseball-kim
4-baseball-gim
5-basketball-ali
6-basketball-kali
7-basketball-kali

will return

Football
Tim
Jim

Baseball
Kim
Gim

basketball
ali
kali

Hope it helps you somehow!

Zaphod Beeblebrox

9:33 am on Jan 11, 2005 (gmt 0)

10+ Year Member



Why do you use GROUP BY when you really want ORDER BY?

SELECT game, player FROM MyTable ORDER BY game