0

I have this line

$sql_events = mysql_query("SELECT team FROM weekpicks WHERE whatweek='$totalnoOfWeek'-1 and team in ( SELECT team1 FROM weekslosers WHERE whatweek='$totalnoOfWeek'-1 ) ORDER BY 'username' asc ")

It works, but I need it to also check team2, team3 and so on. I thought I could do something like the following, but it does not work. it appears to stop at team1

$sql_events = mysql_query("SELECT team FROM weekpicks WHERE whatweek='$totalnoOfWeek'-1 and team in ( SELECT team1 FROM weekslosers WHERE whatweek='$totalnoOfWeek'-1 ) or ( SELECT team2 FROM weekslosers WHERE whatweek='$totalnoOfWeek'-1 ) ORDER BY 'username' asc ")

Or should this be done a different way?

What I am looking to do is take the team from the table weekpicks and compare it to the teams in table weekslosers and the weekslosers table could have up to 8 losing teams so if your team is one of the 8, I need it to be listed.

aumandg
  • 61
  • 5
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 08 '16 at 19:36

1 Answers1

1
SELECT team
FROM weekpicks
INNER JOIN weekslosers ON weekpicks.whatweek = weekslosers.whatweek
WHERE weekpicks.whatweek = (whatever) AND team IN (weekslosers.team1, weekslosers.team2)

Also, you should look into transitioning away from mysql_* and towards mysqli or PDO.

jbafford
  • 5,528
  • 1
  • 24
  • 37
  • I get following Column 'whatweek' in where clause is ambiguous – aumandg Jan 08 '16 at 20:13
  • Updated the answer. Since there are multiple tables with a whatweek column, specifying the table to use (in this case, weekpicks) is necessary. – jbafford Jan 08 '16 at 20:18
  • I just found that in anther post, but what i end up doing is changing the field name in one of the tables to week inlue of whatweek. ty – aumandg Jan 08 '16 at 20:27