dongying7667 2014-03-19 09:39
浏览 50
已采纳

MySQL将3个表与2个表相同

So, am bad at words, i will describe it as in pictures.

This is MATSH table in my database. My goal, is to replace Mang_id, Voistkond_1_id and Voistkond_2_id with names from other tables.

MATSH.Mang_id = MANG.Mang_id

MATSH.Voistkond_1_id and Voistkond_2_id are same as in VOISTKONNAD.Voistkond_id

The all 3 tables have all primary and forgein keys.

MATSH enter image description here

VOISTKONNAD

enter image description here

MANG

enter image description here

But now I am trying to select from table info as the numbers are replace, but then there is a problem. That my Teine_voistkond is taking info from Voistkond_1_id not from Voistkond_2_id...

enter image description here

select MATSH.Matsh_id, MANG.Mang_nimi, MATSH.Voistkond_1_id, VOISTKONNAD.Voistkond_nimi  as Esimene_voistkond, MATSH. Voistkond_2_id, VOISTKONNAD.Voistkond_nimi  as Teine_voistkond, MATSH.Matsh_kuupaev, MATSH.Tulemus, MATSH.Muu_info
from MATSH
join MANG on MATSH.Mang_id = MANG.Mang_id 
join VOISTKONNAD on MATSH.Voistkond_1_id = VOISTKONNAD.Voistkond_id;

But I if i use

...
 join VOISTKONNAD on MATSH.Voistkond_1_id = VOISTKONNAD.Voistkond_id
join VOISTKONNAD on MATSH.Voistkond_2_id = VOISTKONNAD.Voistkond_id;

This gives me

ERROR 1066 (42000): Not unique table/alias: 'VOISTKONNAD'

I tried with aliases. It gives me.

Empty set (0.00 sec)

And with right and left join. It gives me again

Empty set (0.00 sec)

As you probably can understand now, my problem lies in the fact that the Teine_voistkond is not showing the names it should show.

and i simpy find the mistake anymore.

  • 写回答

1条回答 默认 最新

  • dpbf62565 2014-03-19 09:58
    关注
      select MATSH.*, MANG.*, VOISTKONNAD1.* , VOISTKONNAD.*
      from MANG
      INNER JOIN MATSH on MATSH.Mang_id = MANG.Mang_id 
      INNER JOIN VOISTKONNAD on MATSH.Voistkond_1_id = VOISTKONNAD.Voistkond_id
      INNER JOIN VOISTKONNAD AS VOISTKONNAD1 ON MATSH.Voistkond_2_id = VOISTKONNAD1.Voistkond_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画