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 3942854
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:42:46+00:00 2026-05-20T00:42:46+00:00

I have two MySQL tables: One for items and another to log purchases. I’m

  • 0

I have two MySQL tables: One for items and another to log purchases. I’m trying to simply list every product in the items table along with the number of times a specific user has purchased that item. If they have purchased it 0 times I would like for it to still list the item, but say 0. My current query seems exclude records with 0 purchases.

Here’s my current query.. I’ve tried GROUP BY i.item_id as well:

SELECT i.item_id, i.item_name, i.item_sku, i.item_price, COUNT(p.item_id) as purchase_count 
FROM items i 
LEFT OUTER JOIN purchases p 
ON p.item_id = i.item_id 
WHERE p.user_id = '1' 
GROUP BY p.item_id
ORDER BY i.item_name ASC

Here’s the table creation SQL:

CREATE TABLE `items` (
  `item_id` int(11) NOT NULL AUTO_INCREMENT,
  `item_name` varchar(100) NOT NULL,
  `item_sku` varchar(100) NOT NULL,
  `item_price` decimal(19,4) NOT NULL,
  PRIMARY KEY (`item_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

INSERT INTO `items` VALUES(1, 'Item One', 'ITEM1', 99.9900);
INSERT INTO `items` VALUES(2, 'Item Two', 'ITEM2', 19.9900);
INSERT INTO `items` VALUES(3, 'Item Three', 'ITEM3', 10.9900);
INSERT INTO `items` VALUES(4, 'Item Four', 'ITEM4', 4.9900);

CREATE TABLE `purchases` (
  `purchase_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  `purchase_date` datetime NOT NULL,
  PRIMARY KEY (`purchase_id`),
  KEY `user_id` (`user_id`,`item_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `purchases` VALUES(1, 1, 1, '2011-02-01 12:01:38');
INSERT INTO `purchases` VALUES(2, 1, 2, '2011-01-03 12:01:45');
INSERT INTO `purchases` VALUES(3, 1, 1, '2011-02-08 12:02:17');

Thanks for any help! I thought that as a left outer join it would display all items.

  • 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-05-20T00:42:46+00:00Added an answer on May 20, 2026 at 12:42 am

    You will likely need a subquery for this, as with your current query the WHERE clause will constrain your results to rows from the purchase table where the user_id = 1. If you remove the WHERE clause from your query you’ll see what I mean.

    I will update this momentarily with code to support a solution that uses a subquery.

    UPDATE
    Here is the SQL code you will need for your query:

    SELECT i.item_id, i.item_name, i.item_sku, i.item_price, 
    (SELECT COUNT(*) FROM purchases where item_id=i.item_id and user_id='1') as purchase_count 
    FROM items i
    ORDER BY i.item_name ASC
    
    • 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, one containing details on cars and one containing all
In MySQL, I have two tables with a 1:n relationship. Table items has products,
I have two tables. One is items , and one is tags . Query's
I have two identical tables and need to copy rows from table to another.
I have two mysql tables - a sales table: +----------------+------------------------------+------+-----+---------+-------+ | Field | Type
I have two identical tables, one with current values for rows, and one with
I have a MySQL database of 3 tables: I. Person (id, name, purchases) II.
I have two tables: one hostgroup_host and the other one hostgroups. The hostgroups represents
Hi ok I have two tables one called Login and one called User there
I have two tables like below, mysql> select * from Books ; +----+------+------------+----------+----------+ |

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.