简体   繁体   中英

Compare one field from one table to several fields in anther table in mysql via php

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM