Jump to content

sql statement or php array?


vampke

Recommended Posts

Hi guys,

 

I'm working on this script where I want to get data from 2 MySQL tables:

 

table teams:

team_id, team_name

 

table fixtures:

team1_id, team2_id, team1_score, team2_score

 

The query

SELECT * FROM fixtures ORDER BY team1_id, team2_id

works like I want it to, but unfortunately I want to sort the data not on team_id, but alphabetically on team_name.

 

I don't know how to get the tables linked when the team_id is present twice in the linked table.

I'm stuck at:

SELECT f.team1_id, f.team2_id, f.team1_score, f.team2_score, t.team_name FROM fixtures f, teams t WHERE t.team_id=s.team1_id ORDER BY t.team_name";

 

This query actually returns an empty result set, I don't understand why

Probably important: the team id is an integer in the team table but it's a varchar in the fixtures table (i'm trying to adapt an existing script). This cannot be changed since there are already multiple years of data present in the database.

 

I've considered putting everything in arrays and sort them on team name, but that does not solve the team2 order.

 

What would be the easiest solution here?

Link to comment
Share on other sites

FROM fixtures f, teams t WHERE t.team_id=s.team1_id

change to

FROM fixtures f, teams t WHERE t.team_id=f.team1_id

 

hey, thanks for your trouble, this was not the problem.

I did not manage to get a proper query, so I did it with 2 seperate queries and with an array where I checked to see which team name corresponded with the team id in the array.

it works for now, but I think there might be a more elegant solution.

Link to comment
Share on other sites

Hi

 

Joining the tables is elegent, but made a bit less so by having the key fields stored in 2 different formats.

 

SELECT f.team1_id, f.team2_id, f.team1_score, f.team2_score, t.team_name 
FROM fixtures f JOIN teams t ON t.team_id = CONVERT(f.team1_id,signed) 
ORDER BY t.team_name";

 

All the best

 

Keith

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.