Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8443245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:05:17+00:00 2026-06-10T09:05:17+00:00

Alright I am guessing I need a subquery to solve this and I am

  • 0

Alright I am guessing I need a subquery to solve this and I am little rusty on these. So I have 3 tables:

tblAccount – Has User information and AccountID

tblItem – Has Item information and ItemID

tblAccountItem – Has 3 fields – AccountItemID / AccountID / ItemID

An account can have many items and an item can have many accounts. Example data:

tblAccount

AccountID    AccountName     AccountEmail
1            John Smith      john@smith.com
2            Fred John       fred@john.com
3            George Mike     george@mike.com

tblItem

ItemID       ItemName        ItemDescription
1            Hammer          Smashes things
2            Axe             Breaks things

Ok so lets say the Hammer belongs to John,Fred and George. Axe only belongs to John and Fred.

tblAccountItem

AccountItemID       AccountID     ItemID
1                   1             1
2                   2             1
3                   3             1
4                   1             2
5                   2             2

So I want to show what items John has and also show who else owns that item. The output I want to show is:

ItemName            ItemDescription    OtherOwners
Hammer              Smashes things     Fred, George
Axe                 Breaks things      Fred

Any help would be greatly appreciated!

The answer by ctrahey is perfect but I have a slight condition to add. There are 2 types of accounts in tblAccount denoted by a field.

tblAccount

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     

If the AccountTypeID is 1 then I need to output the AccountEmail. If the AccountTypeID is 2 I need to output the AccountDescription. Eg output (same story as above):

ItemName            ItemDescription    OtherOwners
Hammer              Smashes things     Fred, Runner
Axe                 Breaks things      Fred

Going off the query that ctrahey I am guessing there needs to be an ALIAS field created. Something like:

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

I hope this makes sense, thanks for all the help so far!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:05:18+00:00Added an answer on June 10, 2026 at 9:05 am

    Subqueries are very rarely actually needed, and are often replaced (with improved performance) by a well-designed JOIN.

    Here, we start with AccountItem table (the WHERE clause immediately limits the query to only items owned by our account of interest); then we join the same table (aliasing it to ‘others_items_join’), telling it to join with the same itemID but not owned by our account if interest. That’s the essence of the entire query, the next two joins are only to bring in the actual strings we want to be in our output (the other people’s names and the item names/descriptions). GROUP BY is used to give just one row per item which our account of interest has.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright I have the MYSQL query code setup to pull this information out of
Alright so I have no idea how to even begin doing this But basically
Alright, i have read many different views on how to do this with no
Alright, here is my problem i'm trying to solve. I have an index page
Alright. This may be difficult but I have been struggling for quite a bit
Alright, this problem seems to be way above my head! I have this code:
Alright I have a table where I need to get the relevant date from
Alright, I'm new to VBA but I know this has to be possible. I
Alright it has come to this. I searched this website among many others and
Alright, I have this code: jquery //add watermark effect to the specified input box

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.