> I am wanting to select all products that are not visible at all on
> categories with a given name. Say, I want to select all products that are
> not in a category that begins with 'A'. I can select all products not in any
> category at all using
> which is almost right but not quite. Can anyone help ? I am using MySQL.
Does MYSQL support the NOT EXISTS (subquery) construct?
If so, that's probably your best bet.
sks - 23 Jun 2004 15:05 GMT
> > I am wanting to select all products that are not visible at all on
> > categories with a given name. Say, I want to select all products that are
[quoted text clipped - 7 lines]
>
> If so, that's probably your best bet.
Currently, MySQL does not support sub selects at all (the new beta version
does but I'm using this in production).
SELECT * FROM product
MINUS
select p.*
from product p join categories_contents co on
p.id=co.product_id join categories c on co.category_id=c.id
c.name like A%'
> I have a Product table, a Categories table and a join table that contains
> product to category mappings (each product can be in many categories)
[quoted text clipped - 35 lines]
>
> which is almost right but not quite. Can anyone help ? I am using MySQL.
You need to stop using MySQL AND get the real thing. For example, the
DEFAULT clause comes after the datatype in SQL; does your dialect
support that? The datatypes are proprietary, etC.
Please read ANY book on relational design so you will stop using that
"multi-purpose, universal, cures all ills" auto-increment id columns
on every table.
Learn what a key is; this is vital for any data integrity. The same
attribute has different names in the same schema!! Let's look at the
tables AND try to correct the columns to ISO-11179 standards.
Have you ever seen a category name that long? Don't worry, with a
design this poorly done, you will! Take the time to do it right AND
get real keys AND reasonable declarations.
CREATE TABLE Categories
(category_code INTEGER NOT NULL PRIMARY KEY,
category_name CHAR(30) NOT NULL);
Did you really have no industry standard code for your products? Have
you ever seen a product name that long?
CREATE TABLE Products
(upc CHAR(10) NOT NULL PRIMARY KEY,
product_name VARCHAR(30) NOT NULL);
Can we assume a product falls in one AND only one category? Want to
enforce that rule?
CREATE TABLE CategoryContents
(upc INTEGER CHAR(10) NOT NULL UNIQUE
REFERENCES Products (upc)
ON DELETE CASCADE
ON UPDATE CASCADE,
category_code INTEGER NOT NULL
REFERENCES Categories (category_code)
ON DELETE CASCADE
ON UPDATE CASCADE,
PRIMARY KEY (upc, category_code));
>> .. SELECT all products that are NOT visible at all in
categories with a given name. <<
Weird query, since the name has nothing to any kind of categorical
structure. Have you used the Dewey Decimal system? That is a good
way to design a category encoding as one example.
Without a "[NOT] EXISTS()" OR a "[NOT] IN ()" predicate, you cannot do
this easily in one query. I do not know enough about pseudo-sql file
systems like MySQL to help you. You might want to post those
newsgroups.
Try something like this in steps:
INSERT INTO Alpha (category_code, category_name, flag)
SELECT category_code, category_name, 'A'
FROM Categories
WHERE category_name LIKE 'A%'
UNION ALL
SELECT category_code, category_name, 'Z'
FROM Categories
WHERE category_name NOT LIKE 'A%';
If you have a CASE expression use it instead of UNION ALL.
Join this to CategoryContents and get the upc codes and flags. Do a
GROUP BY where MIN(flag) = 'Z' and MAX (flag)= 'Z';
You need to stop using MySQL AND get the real thing. For example, the
DEFAULT clause comes after the datatype in SQL; does your dialect
support that? The datatypes are proprietary, etC.
Please read ANY book on relational design so you will stop using that
"multi-purpose, universal, cures all ills" auto-increment id columns
on every table.
Learn what a key is; this is vital for any data integrity. The same
attribute has different names in the same schema!! Let's look at the
tables AND try to correct the columns to ISO-11179 standards.
Have you ever seen a category name that long? Don't worry, with a
design this poorly done, you will! Take the time to do it right AND
get real keys AND reasonable declarations.
CREATE TABLE Categories
(category_code INTEGER NOT NULL PRIMARY KEY,
category_name CHAR(30) NOT NULL);
Did you really have no industry standard code for your products? Have
you ever seen a product name that long?
CREATE TABLE Products
(upc CHAR(10) NOT NULL PRIMARY KEY,
product_name VARCHAR(30) NOT NULL);
Can we assume a product falls in one AND only one category? Want to
enforce that rule?
CREATE TABLE CategoryContents
(upc INTEGER CHAR(10) NOT NULL UNIQUE
REFERENCES Products (upc)
ON DELETE CASCADE
ON UPDATE CASCADE,
category_code INTEGER NOT NULL
REFERENCES Categories (category_code)
ON DELETE CASCADE
ON UPDATE CASCADE,
PRIMARY KEY (upc, category_code));
>> .. SELECT all products that are NOT visible at all in
categories with a given name. <<
Weird query, since the name has nothing to any kind of categorical
structure. Have you used the Dewey Decimal system? That is a good
way to design a category encoding as one example.
Without a "[NOT] EXISTS()" OR a "[NOT] IN ()" predicate, you cannot do
this easily in one query. I do not know enough about pseudo-sql file
systems like MySQL to help you. You might want to post those
newsgroups.
Try something like this in steps:
INSERT INTO Alpha (category_code, category_name, flag)
SELECT category_code, category_name, 'A'
FROM Categories
WHERE category_name LIKE 'A%'
UNION ALL
SELECT category_code, category_name, 'Z'
FROM Categories
WHERE category_name NOT LIKE 'A%';
If you have a CASE expression use it instead of UNION ALL.
Join this to CategoryContents and get the upc codes and flags. Do a
GROUP BY where MIN(flag) = 'Z' and MAX (flag)= 'Z';