Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / General DB Topics / General DB Topics / June 2004

Tip: Looking for answers? Try searching our database.

Left Outer Join on Join Table with Clause

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sks - 23 Jun 2004 07:58 GMT
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)

CREATE TABLE categories (
 id bigint(20) unsigned NOT NULL auto_increment,
 name varchar(255) NOT NULL default '',
 PRIMARY KEY  (id),
)

CREATE TABLE categories_contents (
 id bigint(20) unsigned NOT NULL auto_increment,
 product_id bigint(20) NOT NULL default '0',
 category_id bigint(20) NOT NULL default '0',
 PRIMARY KEY  (id)
)

CREATE TABLE product (
 id int(10) unsigned NOT NULL auto_increment,
 name varchar(150) NOT NULL default '',
 PRIMARY KEY  (id)
)

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

select p.* from product as p left outer join categories_contents as co on
p.id=co.product_id where co.id is null

However, I cannot seem to get my query right when I need to expand this to
include the categories table so that I can add a clause on the site_id
column. I did the following:

select p.* from product as p left outer join categories_contents as co on
p.id=co.product_id left outer join categories as c on co.category_id=c.id
and c.name like 'A%' where co.id is null

which is almost right but not quite. Can anyone help ? I am using MySQL.
Laconic2 - 23 Jun 2004 14:33 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
> 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).
Isaac Blank - 23 Jun 2004 21:40 GMT
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.
--CELKO-- - 24 Jun 2004 00:56 GMT
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';
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.