dqrm8199 2015-02-06 15:41
浏览 37

在mysql中连接两个表,给出重复的结果

I am trying to connect 2 tables in mysql. But, I am getting duplicate values. This SQLFIDDLE will clear what I meant to say.

In result, you can see the TW_ID and PFC_ID. I just need (1, 1), (11, 11), (21, 21) and so on.. Right now I am getting (1, 1), (1, 11), (1, 21), ...

I am using this query:

SELECT tw.id as tw_id, pfc.id as pfc_id
FROM account_treasury_wholesale tw
INNER JOIN purchase_foreign_currency pfc
    ON tw.wholesaler_id = pfc.currency_supplier_id
WHERE tw.wholesaler_id='11'

Let me know, if you need more clarification. Thanks.

  • 写回答

1条回答 默认 最新

  • douhezi2285 2015-02-06 16:24
    关注

    Either you JOIN ON tw.id=pfc.id, or if that's not applicable you may need to revise your schema.

    This is what happens when you JOIN ON tw.wholesaler_id = pfc.currency_supplier_id:

    1. The first row retrieved from the tw table is tw.id=1 (because of your WHERE clause)
    2. That row's tw.wholesaler_id is 11, so it will look for rows in the pfc table for those with pfc.currency_supplier_id also equal to 11 -- which is 4 rows (id equal to 1, 11, 21, 23). So the cross product will be (1, 1), (1, 11), (1,21), (1,23)
    3. The next row from the tw table is retrieved and the same search happens with the pfc table, which will lead to (11,1), (11,11), (11,21), (11,23)...

    I admit I don't fully understand what you're trying to do. A little more explanation might help.

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来