Jump to content

query/array question....


mkosmosports

Recommended Posts

I have a mysql table that consists of the following columns: START_DATE, TEAM_ID.
What I want to do is run a query that retrieves distinct START_DATE values and then all of the distinct TEAM_ID values that are in the same result row as the distinct START_DATE values. In case Im being a little confusing, in mysql I have:
|START_DATE|TEAM_ID|
|200608|6|
|200608|6|
|200608|7|
|200608|7|
|200609|4|
|200609|4|

What Id like to do then is retrieve the unique dates, and the unique teamids for that date, so something like:|200608 => 6,7|200609 => 4|

This is what I have so far:
$test3 = @mysql_query("SELECT DISTINCT DATE_FORMAT(START_DATE,'%Y%m') AS date, TEAM_ID AS teamid from sf_team_player WHERE OLDCLUB_ID IS NOT NULL ORDER BY START_DATE DESC");
$rows = mysql_num_rows($test3);

for($i=0;$i<$rows;$i++)
{
$tid = mysql_result($test3,$i,'teamid');
$date = mysql_result($test3,$i,'date');
$testarr[$date] = $tid;
}

The above returns an array telling me the number of teamids (theyre not unique though, which is part of the problem) that exist whenever the START_DATE is $date. Now how can I bring that teamid number down by ignoring duplicates (Im having trouble adjusting my query for that, Im pretty sure that is where the problem is) and creating a multidimensional query that will not only tell me the number of unique teamids whenever START_DATE is $date but also show them to me?

Help much appreciated...
mkosmosports
Link to comment
https://forums.phpfreaks.com/topic/34366-queryarray-question/
Share on other sites

I'm surprised that the teamids are not unique from that query.  DISTINCT should return unique rows only.

To get the structure you want, I would use
[code=php:0]$testarr[$date][] = $tid[/code]

Then you get the teamids for each date in an array.

I'm interested to see what your actual output is from that query.
Link to comment
https://forums.phpfreaks.com/topic/34366-queryarray-question/#findComment-161779
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.