Author Topic: query woes  (Read 377 times)

0 Members and 1 Guest are viewing this topic.

Offline vampkeTopic starter

  • Enthusiast
  • Posts: 69
    • View Profile
query woes
« on: May 01, 2009, 10:04:24 AM »
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

Code: [Select]
CREATE TABLE `schedule` (
  `id` int(8) 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`)
)

Code: [Select]
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
Code: [Select]
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?
« Last Edit: May 01, 2009, 10:12:02 AM by vampke »

Offline Ken2k7

  • Freak!
  • Posts: 5,174
    • View Profile
Re: query woes
« Reply #1 on: May 01, 2009, 10:11:03 AM »
What is teams? There's no such table and you're selecting from it.

Try this
Code: [Select]
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
« Last Edit: May 01, 2009, 10:11:53 AM by Ken2k7 »
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.

Offline revraz

  • Freak!
  • Posts: 6,866
  • Gender: Male
  • Problems Resolved: 352
    • View Profile
Re: query woes
« Reply #2 on: May 01, 2009, 10:26:16 AM »
teams is his second table.
If I can't make it better, then I'll make it worse, but it definitely won't be the same.

Offline Ken2k7

  • Freak!
  • Posts: 5,174
    • View Profile
Re: query woes
« Reply #3 on: May 01, 2009, 10:43:24 AM »
teams is his second table.
That's because he edited his post.
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.

Offline vampkeTopic starter

  • Enthusiast
  • Posts: 69
    • View Profile
Re: query woes
« Reply #4 on: May 01, 2009, 01:20:03 PM »
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
Code: [Select]
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:
Code: [Select]
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?

Offline Ken2k7

  • Freak!
  • Posts: 5,174
    • View Profile
Re: query woes
« Reply #5 on: May 01, 2009, 01:27:02 PM »
You never selected them. In your SELECT statement, add t.name and k.name.

Code: [Select]
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
Quote from: Slaytanist
A programmer who shys away from elegant tricks will never be more than competent at best. Ego and a desire to attempt the impossible are traits of most great coders.