Jump to content

[SOLVED] Tough mysql query...


galvin

Recommended Posts

I have a table called "matchups" where there is a "team1" field and "team2" field (and a "matchupID" field which I think is insignificant for this question).  Anyway, for example, let's say I have ten rows in the table like this...

 

matchupID....team1.....team2

 

1............... Eagles.....Steelers

2................Eagles.....Steelers

3................Cardinals..Steelers

4................Eagles......Ravens

5................Cardinals..Steelers

6................Eagles.....Steelers

7................Eagles.....Steelers

8................Cardinals..Ravens

9................Cardinals..Steelers

10...............Eagles......Steelers

 

What I would want is to list out the most popular matchups, in order by the most to the least.

 

So, I would want the output to be as follows...

 

Eagles - Steelers (This one is first because there are 5 of them)

Cardinals - Steelers (This is next because there are 3)

Eagles - Ravens 1 (only one of these)

Cardinals - Ravens 1 (only one of these)

 

So, how do I structure the mysql query to do this?  I thought it would be something like this, but I think I'm way off...

 

$query = "SELECT team1, team2

COUNT(*) AS count,

FROM matchups

WHERE team1 = team1

AND team2 = team2

ORDER BY count DESC";

 

Any help would be GREATLY appreciated.  And just so you know, making a unique ID for each matchup is NOT an option.  My example was simple, but in my real table, there will be THOUSANDS of possible matchup combinations.....

Link to comment
Share on other sites

Try this, make logical sense but havent tested it:

 

Well you should. It's not even proper syntax. left alone any logic...

 

SELECT team1, team2, COUNT(*) AS count FROM matchups GROUP BY CONCAT(team1,team2)

 

 

 

Link to comment
Share on other sites

Strictly speaking, whenever you CONCAT, I would probably recommend CONCAT_WS(), and use a separator that is unlikely to appear in your data.  Otherwise, if you have teams like:

 

team1 = me

team2 = another

 

and

 

team1 = mean

team2 = other

 

then CONCAT() with return the same string for both!  Case insensitivity can also wreak havoc.

 

I would recommend:

 

SELECT team1, team2, COUNT(*) AS count FROM matchups GROUP BY CONCAT_WS( '~',team1,team2)

 

Just to be safe.  You'll get burned eventually.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

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

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.