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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:01:28+00:00 2026-06-01T17:01:28+00:00

I am attempting to write a MySQL query that will return element whose details

  • 0

I am attempting to write a MySQL query that will return element whose details meet a this but not this criteria.

As an example consider a an inventory management system for office supplies. Supplies of all types would be contained within this database, and instead of creating a table with a myriad of columns each attribute of a product’s description I instead did this.

CREATE TABLE `inventory`(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
element_name VARCHAR(255) NOT NULL
);

CREATE TABLE `inventory_detail`(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
element_id INTEGER NOT NULL,
label VARCHAR(255) NOT NULL,
attribute_string VARCHAR(255) NULL,
attribute_integer INTEGER NULL
);

Using these tables each product can added as inventory and it’s attributes can be added as inventory detail. (ie. label = color, attribute_string = red or label = color, attribute_string != blue)
Don’t critique this method it is not what my question is about. Besides this method allows me to constantly change the number of details about a product that is stored while not having to change the database in any way.

My question is what should my query look like if I wanted to get a list of all green products that have a positive count. Basically I am looking for a query that returns element.id and the elments must be sorted by details that are excepted and ones that are not. For example:

SELECT element.id
FROM element
INNER JOIN element_detail ON element.id = element_detail.id
WHERE (element_detail.label = 'color'
AND element_detail.attribute_string = 'green')
AND (element_detail.label = 'count'
AND element_detail.attribute_integer <> 0);

####################################
#             INVENTORY            #
####################################
# id #           label             #
####################################
#  1 #   multi color pen pack      #
#  2 #   single pen pack (red)     #
#  3 #   single pen pack (green)   #
#  4 #   single pen pack (black)   #
#  5 #   single pen pack (blue)    #
#  6 #   single pen pack (purple)  #
####################################

########################################################
#                  INVENTORY_DETAILS                   #
########################################################
# id # element_id #    label   #    attribute_string   #
########################################################
#  1 #     1      #    color   #          red          #
#  2 #     1      #    color   #          blue         #
#  3 #     1      #    color   #          black        #
#  4 #     1      #    color   #          green        #
#  5 #     1      #    color   #          red          #
#  6 #     1      #    count   #          100          #
#  7 #     2      #    color   #          red          #
#  8 #     2      #    count   #          50           #
#  9 #     3      #    color   #          green        #
# 10 #     3      #    count   #          50           #
# 11 #     4      #    color   #          black        #
# 12 #     4      #    count   #          50           #
# 13 #     5      #    color   #          blue         #
# 14 #     5      #    count   #          0            #
# 15 #     5      #    color   #          purple       #
# 16 #     5      #    count   #          50           #
########################################################

A query that will return all products in the inventory and their count that have the color red but not green.

########################################
#              RESULT                  #
########################################
#       PRODUCT             #   COUNT  #
#  single pen pack (red)    #     50   #
#  single pen pack (black)  #     50   #
#  single pen pack (blue)   #     0    #
#  single pen pack (purple) #     50   #
########################################

While the mutli color pen pack has a red color pen it also has a green color pen, and should not be included in the result.

  • 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-01T17:01:29+00:00Added an answer on June 1, 2026 at 5:01 pm

    If you want all “green” products with a positive count then you will need to join twice on the detail table

    SELECT element.id
    FROM element
    INNER JOIN element_detail e1 ON element.id = e1.id
    INNER JOIN element_detail e2 ON element.id = e2.id
    WHERE (
          e1.label = 'color'
      AND e1.attribute_string = 'green'
    ) AND (
          e2.label = 'count'
      AND e2.attribute_integer > 0
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm completely new to MySQL. I'm attempting to write a query that inserts a
I'm attempting to write a script that returns rows from a mySQL database. Basically,
I am attempting to write a one-line Perl script that will toggle a line
I am attempting to write a firefox addon that will analyze the displayed page
I'm (attempting) to write a MySQL stored procedure that parses a large text file.
I am attempting to write an ASP.net web service that will be utilized by
I'm attempting to write a function in assembly that will detect if a longer
I've been attempting to write a Lisp macro that would perfom the equivalent of
I am attempting to write an application that uses libCurl to post soap requests
I'm attempting to write a Python C extension that reads packed binary data (it

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.