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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:43:02+00:00 2026-06-14T06:43:02+00:00

I have two examples of tables I am using I am using the output

  • 0

I have two examples of tables I am using
I am using the output to build arrays so my output needs to be a single column

First table is a list of products

    PRODUCT ID    PRODUCT_NAME
    01            product1
    02            product2
    03            product3
    04            product4
    05            product5
    06            product6
    07            product7
    08            product8
    09            product9
    10            product10
    11            product11
    12            product12

Table two is a list of purchases

    SALESID       PRODUCT_NAME       USERID       DATE
    01            product1           001ABC       11-10-2012
    02            product5           001ABC       11-11-2012
    03            product1           002XYZ       11-11-2012
    04            product12          001ABC       11-13-2012
    05            product7           001ABC       11-13-2012
    06            product3           002XYZ       11-14-2012
    07            product11          002XYZ       11-14-2012

Now what I need is if I supply a USERID like 001ABC
I need the output to be a 1 if the USERID matches the one I supply and(which it should automatically)it appears in the product table and I need it to show a 0 if it just appears in the products table

So two examples of the above two tables and these two USERIDs would look like this

USERID 001ABC supplied
yields

  OUTPUT
  1
  0
  0
  0
  1
  0
  1
  0
  0
  0
  0
  1

USERID 002XYZ supplied
yields

  OUTPUT
  1
  0
  1
  0
  0
  0
  0
  0
  0
  0
  1
  0

Using CASE statements has failed or I was not getting the syntax correct
I am using PHP and MYSQL

My current query worked good as long as I had a single USERID but failed when I tried filtering by the userID as well

PHP/MYSQL query:

    SELECT (PURCHASES.PRODUCT_NAME IS NOT NULL) AS purchased 
    FROM PRODUCTS 
      LEFT JOIN PURCHASES 
      ON PRODUCT_NAME = PRODUCT_NAME ORDER BY 1

I am not sure what I am doing wrong.

When I added another user with two purchases the results seemed to simply add two more rows to the output so the output would have had 14 rows instead of the original 12 as in the examples I show.

How can I can get a single column output with a total amount of records that equals the total amount of products each row set to zero except when a match is found in the purchases table identified by the product name then instead of zero a 1 is in the output.

  • 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-14T06:43:03+00:00Added an answer on June 14, 2026 at 6:43 am

    Here’s the query you want. You need to order the results by the product ID; you’re ordering them by the 0/1 value in the output column, which of course destroys the usefulness of that column.

    You also need to do a summary on the filtered purchases table to figure out whether there have been any purchases of the specific product. That’s the subquery “b”.

    Finally, you left join from products to purchases, because you want exactly one row in your output resultset for each product.

         select ifnull(b.number>0,0) output
           from products a
      left join (
                  select count(*) number,
                         product_name
                    from purchases
                   where userid = '001ABC'
                group by product_name
               ) b on a.product_name = b.product_name
      order by a.product_id
    

    See this SQL Fiddle for example. http://sqlfiddle.com/#!2/6e985/12/0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using two tables for my current project(it will grow certainly). First table has
I have two tables: tab1 and tab2. Every table has only one VARCHAR(MAX) column.
I have two tables, for example: Table A Table B ======= ======= Name |
I have two separate tables TVs and Receivers that I am using the FOR
i have a table where termid,termversion are two primary key. column present in table
I am using List::Compare to compare two files and print out the output in
Let us say I have two table types (tables of object types) that I
I have two data.table s in R: > tables() NAME NROW MB COLS KEY
I have two projects using legacy databases with no associations between the tables. In
I have two master tables CompanyMaster , ActivityMaster for a child table CompanyActivities ActivityMaster

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.