Jump to content

query woes


vampke

Recommended Posts

Hello peoples,

 

I trying to get this query to work but I'm stuck like a madman in a straitjacket :/

 

I have 2 tables: teams and schedule

 

CREATE TABLE `schedule` (
  `id` int( unsigned zerofill NOT NULL auto_increment,
  `team1` varchar(50) NOT NULL default '',
  `team2` varchar(50) NOT NULL default '',
  `team1_score` varchar(5) NOT NULL default '0',
  `team2_score` varchar(5) NOT NULL default '0',
  PRIMARY KEY  (`id`)
)

 

CREATE TABLE IF NOT EXISTS `teams` (
  `id` int(4) unsigned zerofill NOT NULL auto_increment,
  `name` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`id`)
)

 

team1 and team2 form schedule table are id's from the teams table

 

I need the results from the schedule table ordered alphabetically by team1.name and then by team2.name

 

so far I have

SELECT s.team1, s.team2, s.team1_score, s.team2_score , t.name
FROM schedule s, teams t
WHERE s.team1 = t.id
ORDER BY t.name

 

But I don't know how to get the teamname for team2 this way.

 

Can anyone help me with this?

Link to comment
Share on other sites

What is teams? There's no such table and you're selecting from it.

 

Try this

SELECT s.id AS sid, * FROM schedule s INNER JOIN zclpN_teams t ON t.id = s.team1 INNER JOIN zclpN_teams k ON k.id = s.team2 ORDER BY t.name, k.name

Link to comment
Share on other sites

yes i edited my OP

I have been fiddling around with your code ken, thanks for it, it was really helpful.

 

I have nearly the result i need using

SELECT s.id, s.team1, s.team2, s.team1_score, s.team2_score
FROM schedule s
INNER JOIN teams t ON t.id = s.team1
INNER JOIN teams k ON k.id = s.team2
ORDER BY t.name, k.name

 

The only thing i need now is a way to get t.name and k.name.

 

I uses the following php code:

while ($count < $total_rows) {
	$team1 = mysql_result($resultset,$count,"team1");
	$team2 = mysql_result($resultset,$count,"team2");
	$t1_score = mysql_result($resultset,$count,"team1_score");
	$t2_score = mysql_result($resultset,$count,"team2_score");

 

I tried different things to get the t.name and k.name in my result set but without luck.

 

Any ideas on this?

Link to comment
Share on other sites

You never selected them. In your SELECT statement, add t.name and k.name.

 

SELECT s.id, s.team1, s.team2, s.team1_score, s.team2_score, t.name, k.name
FROM schedule s
INNER JOIN teams t ON t.id = s.team1
INNER JOIN teams k ON k.id = s.team2
ORDER BY t.name, k.name

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.