Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AccountID AccountName AccountEmail
- 1 John Smith john@smith.com
- 2 Fred John fred@john.com
- 3 George Mike george@mike.com
- ItemID ItemName ItemDescription
- 1 Hammer Smashes things
- 2 Axe Breaks things
- AccountItemID AccountID ItemID
- 1 1 1
- 2 2 1
- 3 3 1
- 4 1 2
- 5 2 2
- ItemName ItemDescription OtherOwners
- Hammer Smashes things Fred, George
- Axe Breaks things Fred
- 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
- AccountTypeID AccountType
- 1 User
- 2 Admin
- ItemName ItemDescription OtherOwners
- Hammer Smashes things Fred, Runner
- Axe Breaks things Fred
- WHERE AccountTypeID = 1 (SELECT AccountName)
- WHERE AccountTypeID = 2 (SELECT AccountDescription)
- 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
- SELECT AccountName
- FROM tblAccount
- WHERE AccountID = (SELECT AccoundID
- FROM tblAccountItem
- WHERE itemID = (SELECT itemID
- FROM tblAccountItem
- WHERE AccountID = 1 (john Id as example)));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement