我有三个表,我只列出重要的列
db_players
id |名称
玩家
id | teamid | careerid
db_teams
db_teams id链接到玩家teamid。
我需要运行一个查询,我从db_players中选择行,只要db_teams.id不在作为teamid的行中,其中careerid = 1。
我之前从未尝试过使用mysql这种类型的查询,我知道我可以做两个查询并涉及php,但我很想知道是否可以使用纯数据库查询。
感谢。
编辑 - 现在更简单。
SELECT dp.first_name
FROM tbl_foot_career_db_players dp
INNER JOIN tbl_foot_career_players p
ON p.playerid != dp.id
WHERE p.careerid = 1
我的想法是,我想从tbl_foot_career_db_players返回所有行WHERE该表中的id不存在于playerid列中tbl_foot_career_players的行中。并且tbl_foot_career_players.careerid也必须等于1。
答案 0 :(得分:1)
尝试使用db_players来加入它们id不在玩家队伍中。
SELECT db_players.* FROM db_players
LEFT JOIN players ON players.id != db_players.id
LEFT JOIN db_teams ON players.teamid = db_teams.id
WHERE careerid = 1
答案 1 :(得分:1)
列出所有不属于职业= 1
的玩家的db_playersSELECT d.*
FROM db_players d
LEFT JOIN players p
ON p.player_id = d.id
AND p.career = 1
WHERE p.id IS NULL