MySQL子查询概述

MySQL子查询概述,mysql,subquery,Mysql,Subquery,好吧,我猜我需要一个子查询来解决这个问题,我对这些有点生疏。所以我有3张桌子: tblAccount tblItem tblAccountItem tblAccount-具有用户信息和帐户ID tblItem-具有项目信息和项目ID AccountID-有3个字段-AccountItemID/AccountID/ItemID 一个帐户可以有多个项目,一个项目可以有多个帐户。示例数据: tblAccount AccountID AccountName AccountEmail 1

好吧,我猜我需要一个子查询来解决这个问题,我对这些有点生疏。所以我有3张桌子:

tblAccount
tblItem
tblAccountItem

tblAccount-具有用户信息和帐户ID
tblItem-具有项目信息和项目ID
AccountID-有3个字段-AccountItemID/AccountID/ItemID

一个帐户可以有多个项目,一个项目可以有多个帐户。示例数据:

tblAccount

AccountID    AccountName     AccountEmail
1            John Smith      john@smith.com
2            Fred John       fred@john.com
3            George Mike     george@mike.com
AccountID    AccountName     AccountEmail       AccountDescription    AccountTypeID
1            John Smith      john@smith.com     NULL                  1
2            Fred John       fred@john.com      NULL                  1
3            George Mike     george@mike.com    Runner                2
tblItem

ItemID       ItemName        ItemDescription
1            Hammer          Smashes things
2            Axe             Breaks things
好吧,假设这把锤子属于约翰、弗雷德和乔治。斧头只属于约翰和弗雷德

tblAccountItem

AccountItemID       AccountID     ItemID
1                   1             1
2                   2             1
3                   3             1
4                   1             2
5                   2             2
所以我想展示John拥有哪些物品,同时也展示谁拥有这些物品。我想显示的输出是:

ItemName            ItemDescription    OtherOwners
Hammer              Smashes things     Fred, George
Axe                 Breaks things      Fred
任何帮助都将不胜感激

ctrahey的答案是完美的,但我有一点需要补充。tblAccount中有两种类型的帐户,由字段表示

tblAccount

AccountID    AccountName     AccountEmail
1            John Smith      john@smith.com
2            Fred John       fred@john.com
3            George Mike     george@mike.com
AccountID    AccountName     AccountEmail       AccountDescription    AccountTypeID
1            John Smith      john@smith.com     NULL                  1
2            Fred John       fred@john.com      NULL                  1
3            George Mike     george@mike.com    Runner                2
tblAccountTypeID

AccountTypeID   AccountType     
1               User      
2               Admin     
如果AccountTypeID是1,那么我需要输出AccountEmail。如果AccountTypeID为2,我需要输出AccountDescription。Eg输出(同上):

离开ctrahey的查询,我猜需要创建一个别名字段。比如:

WHERE AccountTypeID = 1 (SELECT AccountName)
WHERE AccountTypeID = 2 (SELECT AccountDescription)

我希望这是有意义的,谢谢你迄今为止的帮助

您最好使用编码来解决这个问题,这里的粗略查询可能会帮助您了解:

SELECT AccountName
FROM tblAccount
WHERE AccountID = (SELECT AccoundID 
                   FROM tblAccountItem
                   WHERE itemID = (SELECT itemID
                                   FROM tblAccountItem
                                   WHERE AccountID = 1 (john Id as example)));

希望这有帮助

子查询实际上很少需要,并且经常被精心设计的联接所取代(性能得到提高)

这里,我们从AccountItem表开始(WHERE子句立即将查询限制为仅限于我们感兴趣的帐户拥有的项目);然后我们加入同一个表(将其别名为“others\u items\u join”),告诉它加入相同的itemID,但如果感兴趣,则不属于我们的帐户。这是整个查询的本质,接下来的两个连接只会引入我们想要在输出中使用的实际字符串(其他人的姓名和项目名称/描述)。GROUP BY用于为我们的利息账户中的每个项目提供一行

SELECT 
  ItemName,
  ItemDescription,
  GROUP_CONCAT(others.AccountName) as OtherOwners
FROM 
  tblAccountItem as my_items
  LEFT JOIN tblAccountItem as others_items_join 
    ON others_items_join.ItemID = my_items.ItemID AND others_items_join.AccountID != ?
  LEFT JOIN tblAccount as others
    ON others_items_join.AccountID = others.AccountID
  JOIN tblItems ON my_items.ItemID = tblItems.ItemID
WHERE my_items.AccountID = ?
GROUP BY ItemName

从tblitem中选择ItemName、itemsdescription、AccountItemID右键连接tblitem上的tblaccountitem.ItemID=tblaccountitem.ItemID


我希望这有助于得出你的答案。我仍在研究此问题

我运行此程序并得到一个错误:错误代码:1242。子查询返回超过1行,感谢您的快速回答!看起来可能就是这样!现在只是测试一下,但到目前为止似乎很完美。谢谢你的回答。在测试中它工作得很好。由于上面的例子是从我的项目中简化出来的,所以我在问题中添加了一个小问题。