UNION MySQL的问题

时间:2014-10-30 20:34:42

标签: mysql sql join union

这很好但现在我想加入第三张桌子,名为" houses"。但仅当houses.owner等于p.id时。如果没有houses.owner等于p.id则不要选择。

当前可行的SQL

-- Select player AND if he is member of a guild then get rank and guild
SELECT p.id, p.account_id, p.group_id, p.name, p.sex, p.level, p.vocation, p.town_id, p.lastlogin, p.comment, p.deleted, p.online, p.rank_id, gr.name AS rank, g.name AS guild
      FROM players p 
      INNER JOIN guild_ranks gr ON gr.id = p.rank_id 
      INNER JOIN guilds g ON g.id = gr.guild_id 
      WHERE p.name = :name and p.rank_id > 0
      UNION ALL
          SELECT p.id, p.account_id, p.group_id, p.name, p.sex, p.level, p.vocation, p.town_id, p.lastlogin, p.comment, p.deleted, p.online, p.rank_id, '' AS rank, '' AS guild
          FROM players p
          WHERE p.name = :name AND p.group_id < 5 AND p.rank_id = 0

我尝试过添加:

但它给了我:

The used SELECT statements have a different number of columns

1 个答案:

答案 0 :(得分:0)

p.rank_id,'AS等级','AS公会

问题出在第二个问题上。

第一个查询有3列,其中是2.添加另一列,它将起作用。看来你需要一个等级和一个公会,在那里你是使用字符串作为等级的支持公会