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

  • Home
  • SEARCH
  • 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 8108965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:16:25+00:00 2026-06-06T01:16:25+00:00

I have 3 tables: items , purchases , and collaborators . A user can

  • 0

I have 3 tables: items, purchases, and collaborators. A user can own an item, purchase an item, or be a collaborator on an item. Additionally, items that are purchased can be rated up, +1, or down, -1. An owner or collaborator can’t purchase their own item.

I’d like to get all items for a given user and also display the ratings on each item.

Here’s my tables:

      items           |           purchases           |     collaborators
i_id  item_id user_id | p_id item_id  user_id  rating |c_id item_id user_id
  1      1       11   |  1      1         13     -1   |  1     1        12 
  2      2       12   |  2      2         11      1   |  2     2        13
  3      3       13   |  3      3         12     NULL |    
                      |  4      1         14     -1   |

Here’s my MYSQL query so far:

select *, count(p_id) as tots, sum(rating=1) as yes, sum(rating= '-1') as no
from items
left join purchases
on items.item_id=purchases.item_id
left join collaborators
on items.item_id=collaborators.item_id
where items.user_id=13 or purchases.user_id=13 or collaborators.user_id=13
group by items.item_id

Here’s my expected results for user_id=11 (changing each user_id in the WHERE clause):

item_id    tots  yes no
  1         2     0   2
  2         1     1   0
// notice how user_id=11 doesn't have anything to do with item_id=3

Here’s my expected results for user_id=12:

item_id    tots  yes no
  1         2     0   2
  2         1     1   0    
  3         1     1   0  

Here’s my expected results for user_id=13:

item_id    tots  yes no
  1         2     0   2
  2         1     1   0    
  3         1     1   0   
//notice user_id=13 should have same results as user_id=12. Although, their 
relation to each of the 3 items is different, they still either purchased, 
own, or collaboratored on each of them.

Unfortunately, I get the first two results but not the correct one for user_id=13.
For user_id=13, item_id=1 the tots=1 and not tots=2 for some reason I can’t understand.

Any thoughts, such as, “its better to separate this into 2 queries”, would be greatly appreciated,

  • 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-06T01:16:27+00:00Added an answer on June 6, 2026 at 1:16 am

    I’m still not entirly sure I understand you correct but you could try following statement and let us work from there.

    Edit

    Following statement returns the expected results.
    You can verify this (using SQL Server) here.

    The gist of this is to

    • select all possible user_id and item_id combinations from your three tables
    • select the counts/ratings for each item
    • combine the results

    SQL Statement

    SELECT u.user_id, pt.item_id, pt.cnt, pt.yes, pt.no
    FROM   (
            SELECT user_id, item_id, title FROM items 
            UNION SELECT user_id, item_id, NULL FROM purchases
            UNION SELECT user_id, item_id, NULL FROM collaborators      
           ) u INNER JOIN (
            SELECT COUNT(*) AS cnt
                   , SUM(CASE WHEN ISNULL(rating, 1) = 1 THEN 1 ELSE 0 END) AS yes
                   , SUM(CASE WHEN rating =-1 THEN 1 ELSE 0 END) AS no
                   , item_id
            FROM   purchases
            GROUP BY
                   item_id
          ) pt ON pt.item_id = u.item_id    
    

    MYSQL statement

    SELECT u.user_id, pt.item_id, pt.cnt, pt.yes, pt.no, u.title
    FROM   (
        SELECT user_id, item_id, title FROM items where user_id=13
        UNION SELECT user_id, item_id, NULL FROM purchases where user_id=13
        UNION SELECT user_id, item_id, NULL FROM collaborators where user_id=13       
       ) u INNER JOIN (
        SELECT COUNT(*) AS cnt
               , SUM(rating=1) AS yes
               , SUM(rating =-1) AS no
               , item_id
        FROM   purchases 
        GROUP BY
               item_id
      ) pt ON pt.item_id = u.item_id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two mysql tables: Item containing items that one can buy: CREATE TABLE
I have 2 tables: users and items. Each item has a user#. So, if
I have two MySQL tables: One for items and another to log purchases. I'm
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
I have two tables Items and ItemDescriptions class Item(Base): __tablename__ = item id =
I have a Database that has the following relations: Transaction->Purchase->Item->Schedule Transaction - self explanitory
I have two tables: Items: | Item| A B C userItems: | UID |
I have a purchase form that has a continuous subform which shows line items
I have two tables in a database one named purchases and one named items
I have three tables: sales , which are purchases from a store items ,

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.