Staggan Posted November 29, 2012 Share Posted November 29, 2012 Hello I am trying to get data from another table added into my query to save using another query... here is my current query: SELECT * from challenge where challenge_id = $challengeid AND status = 'completed' || status = 'forfeit' I would like to add name from the team table where team_id = challenge.winner_team_id How would I do this... I am sure it is some kind of join, but when I tried I could not get the name field in my result Any help would be much appreciated Thanks Link to comment https://forums.phpfreaks.com/topic/271342-join-data-from-another-table-into-result/ Share on other sites More sharing options...
Barand Posted November 29, 2012 Share Posted November 29, 2012 SELECT * from challenge INNER JOIN team ON team.team_id = challenge.winner_team_id where challenge_id = $challengeid AND (status = 'completed' OR status = 'forfeit') Link to comment https://forums.phpfreaks.com/topic/271342-join-data-from-another-table-into-result/#findComment-1396150 Share on other sites More sharing options...
Staggan Posted November 29, 2012 Author Share Posted November 29, 2012 Hello, That does what I want, but I need to change it slightly... If challenge.winner_team_id is -1 I still need the results but need to set team.name = -1 So I need.. Everything from challenges based on status, and then IF winner_team_id exists in team table I need name, or I need to set name = -1 Can this be done? Thanks Link to comment https://forums.phpfreaks.com/topic/271342-join-data-from-another-table-into-result/#findComment-1396184 Share on other sites More sharing options...
Staggan Posted November 29, 2012 Author Share Posted November 29, 2012 Ah, LEFT JOIN gave me what I needed instead of INNER JOIN, it set the name field to NULL which I can test for Link to comment https://forums.phpfreaks.com/topic/271342-join-data-from-another-table-into-result/#findComment-1396187 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.