Advertisement
Guest User

Untitled

a guest
Mar 4th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. AccountID AccountName AccountEmail
  2. 1 John Smith john@smith.com
  3. 2 Fred John fred@john.com
  4. 3 George Mike george@mike.com
  5.  
  6. ItemID ItemName ItemDescription
  7. 1 Hammer Smashes things
  8. 2 Axe Breaks things
  9.  
  10. AccountItemID AccountID ItemID
  11. 1 1 1
  12. 2 2 1
  13. 3 3 1
  14. 4 1 2
  15. 5 2 2
  16.  
  17. ItemName ItemDescription OtherOwners
  18. Hammer Smashes things Fred, George
  19. Axe Breaks things Fred
  20.  
  21. AccountID AccountName AccountEmail AccountDescription AccountTypeID
  22. 1 John Smith john@smith.com NULL 1
  23. 2 Fred John fred@john.com NULL 1
  24. 3 George Mike george@mike.com Runner 2
  25.  
  26. AccountTypeID AccountType
  27. 1 User
  28. 2 Admin
  29.  
  30. ItemName ItemDescription OtherOwners
  31. Hammer Smashes things Fred, Runner
  32. Axe Breaks things Fred
  33.  
  34. WHERE AccountTypeID = 1 (SELECT AccountName)
  35. WHERE AccountTypeID = 2 (SELECT AccountDescription)
  36.  
  37. SELECT
  38. ItemName,
  39. ItemDescription,
  40. GROUP_CONCAT(others.AccountName) as OtherOwners
  41. FROM
  42. tblAccountItem as my_items
  43. LEFT JOIN tblAccountItem as others_items_join
  44. ON others_items_join.ItemID = my_items.ItemID AND others_items_join.AccountID != ?
  45. LEFT JOIN tblAccount as others
  46. ON others_items_join.AccountID = others.AccountID
  47. JOIN tblItems ON my_items.ItemID = tblItems.ItemID
  48. WHERE my_items.AccountID = ?
  49. GROUP BY ItemName
  50.  
  51. SELECT AccountName
  52. FROM tblAccount
  53. WHERE AccountID = (SELECT AccoundID
  54. FROM tblAccountItem
  55. WHERE itemID = (SELECT itemID
  56. FROM tblAccountItem
  57. WHERE AccountID = 1 (john Id as example)));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement