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.
If you want all “green” products with a positive count then you will need to join twice on the detail table