Jump to content

Complex JOIN, Subquery


JipThePeople

Recommended Posts

I get an error "#1241 - Operand should contain 1 column(s) " when executing this code:

 

SELECT `game_datetime`, `home_team_temp`, `visiting_team_temp`, (SELECT CONCAT(`mascot`, ' ', `city`) AS `home_team`, `logo_url` AS `home_logo_url` FROM `teams`, `logos`, `schedules` WHERE `teams`.`id` = `logos`.`team_id` AND `teams`.`id` = `schedules`.`home_team_id`), (SELECT CONCAT(`mascot`, ' ', `city`) AS `visiting_team`, `logo_url` AS `visiting_logo_url` FROM `teams`, `logos`, `schedules` WHERE `teams`.`id` = `logos`.`team_id` AND `teams`.`id` = `schedules`.`visiting_team_id`) FROM `schedules`, `teams` WHERE `teams`.`id` = `schedules`.`visiting_team_id` OR `teams`.`id` = `schedules`.`home_team_id`

 

Any suggestions?

Link to comment
Share on other sites

Error is due to that you are selecting more than 1 column in your sub-queries... that is not allowed in that context.

 

Thx for the reply. I am trying to query multiple tables

 

I have a 'teams' table that has columns for 'id' and 'city' + 'mascot' = team name. I have another table, 'schedules' that has records containing the two teams (id's) that are to play. I am trying to accomplish this in a single query rather than two. Any recommendations? Thx again for your time.

 

Link to comment
Share on other sites

SELECT game_datetime, home_team_temp, visiting_team_temp
, CONCAT(h.mascot, ' ', h.city) AS home_team, hl.logo_url AS home_logo_url
, CONCAT(v.mascot, ' ', v.city) AS visiting_team, vl.logo_url AS visiting_logo_url
FROM schedules
INNER JOIN teams AS h ON ( h.id = schedules.home_team_id )
INNER JOIN logos AS hl ON (  hl.id  = schedules.home_team_id )
INNER JOIN teams AS v  ( h.id = schedules.visiting_team_id )
INNER JOIN logos AS hl ON (  hl.id = schedules.visiting_team_id )

Link to comment
Share on other sites

SELECT game_datetime, home_team_temp, visiting_team_temp
, CONCAT(h.mascot, ' ', h.city) AS home_team, hl.logo_url AS home_logo_url
, CONCAT(v.mascot, ' ', v.city) AS visiting_team, vl.logo_url AS visiting_logo_url
FROM schedules
INNER JOIN teams AS h ON ( h.id = schedules.home_team_id )
INNER JOIN logos AS hl ON (  hl.id  = schedules.home_team_id )
INNER JOIN teams AS v  ( h.id = schedules.visiting_team_id )
INNER JOIN logos AS hl ON (  hl.id = schedules.visiting_team_id )

 

Thx, that was exactly what I needed. I had to tweak a things (h1.id, h1 duplicated - changed to v1, v1.id) but it works great. Thanks for the solution!

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.