嵌套的sql JOIN

时间:2017-12-01 09:59:14

标签: sql nested

我只有第一个表(fi.id,fi.user_id,fi.amount,fi.currency,fi.created_at)作为输出,具有以下代码。你能告诉我为什么JOIN无效吗? 谢谢您的帮助 马哈福德

 SELECT fi.id, fi.user_id, fi.amount, fi.currency, fi.created_at
     FROM fund_infos fi
      JOIN (SELECT ii.id, ii.user_id, ii.first_kana_name, ii.last_kana_name
    FROM individual_infos ii) Y ON fi.user_id = Y.id
   ORDER BY fi.id

3 个答案:

答案 0 :(得分:0)

您只返回表fund_infos中的值作为选择的一部分。

要从Y输出列,请在select:

中指定列
 SELECT fi.id, fi.user_id, fi.amount, fi.currency, fi.created_at,
        Y.user_id, Y.first_kana_name, Y.last_kana_name -- You can also use Y.* here
 FROM fund_infos fi
 JOIN (
      SELECT ii.id, ii.user_id, ii.first_kana_name, ii.last_kana_name
      FROM individual_infos ii
      ) Y 
   ON fi.user_id = Y.id
 ORDER BY fi.id

答案 1 :(得分:0)

您需要在SELECT中包含所需的所有列。同样重要的是,您不需要子查询来执行此操作:

SELECT fi.id, fi.user_id, fi.amount, fi.currency, fi.created_at,
       ii.user_id, ii.first_kana_name, ii.last_kana_name
FROM fund_infos fi JOIN
     individual_infos ii
     ON fi.user_id = ii.id
ORDER BY fi.id;

注意:

  • 您不需要在ii.id中加入SELECT,因为您已经拥有fi.user_id
  • 您可能想要重命名user_id字段,因此它们有不同的名称。
  • 您确定JOIN条件是否正确?奇怪的是,这两个表都有iduser_id s。

答案 2 :(得分:0)

使用join

SELECT fi.id, fi.user_id, fi.amount, fi.currency, fi.created_at, ii.id, ii.first_kana_name,
       ii.last_kana_name
FROM fund_infos fi
     JOIN individual_infos ii ON fi.user_id = ii.user_id
ORDER BY fi.id;