简体   繁体   English

连接2个表,并根据其关联ID重复显示同一列

[英]Joining 2 Tables and show the same column repeatedly based on its associative ID

Hi i am not sure how to put this in a brief sentences, but i have DB table like the following 嗨,我不确定如何用简短的句子来表达,但是我有如下的数据库表

User Table 用户表

  • user_id 用户身份
  • username 用户名
  • and so on... 等等...

Item 项目

  • item_id item_id
  • item_name 项目名称

Item_Equipped 装备物品

  • equipped_id head (FK to item_id) furnished_id头(从FK到item_id)
  • hand (FK to item_id) 手(从FK到item_id)
  • user_id (FK to user_id IN User Table) user_id(从FK到用户表中的user_id)

I would like to generate a query that will display like the following format 我想生成一个查询,显示如下格式

user_id | user_id | head | head_item_name | head_item_name | hand | hand_item_name | hand_item_name | ... ...

So far i only able to do this: 到目前为止,我只能做到这一点:

SELECT user.user_id, user.username,
        equipments.head, equipments.head_acc,
        equipments.hand,
        equipments.acc, equipments.body
FROM gw_member_equipped AS equipments
LEFT JOIN gw_member AS user ON user.memberid = equipments.member_id

Which (i have to be brutally honest) doesn't do anything much. 哪个(我必须实话实说)没有什么用。

I tried to perform INNER JOIN between item and item_equipped however i am unable to get individual name for each item (based on its item ID) 我尝试在item和item_equipped之间执行INNER JOIN,但是我无法获取每个项目的个人名称(基于其ID)

you need to join ITEM table two times with ITEM_EQUIPPED table. 您需要将ITEM表与ITEM_EQUIPPED表连接两次。

you can use below query for your desired output column shown in question.. 您可以在下面的查询中使用所需的输出列来显示问题。

SELECT USER.User_Id,
       Item_Equipped.Head,
       Item_Heads.Item_Id Head_Item_Id, -- if you want you can remove this column
       Item_Heads.Item_Name Head_Item_Name,       
       Item_Equipped.Hand,
       Item_Hands.Item_Id Hand_Item_Id,  -- and this column also as these columns are same as their previous select columns
       Item_Hands.Item_Name Hand_Item_Name
  FROM USER, Item Item_Heads, Item Item_Hands, Item_Equipped
 WHERE USER.User_Id = Item_Equipped.User_Id
   AND Item_Heads.Item_Id = Item_Equipped.Head
   AND Item_Hands.Item_Id = Item_Equipped.Hand

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM