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
https://forums.phpfreaks.com/topic/219321-complex-join-subquery/
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.

 

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 )

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!

Archived

This topic is now archived and is closed to further replies.

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