473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Left Outer Join with clause

sks
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 '',
site_id bigint(20) NOT NULL default '0',
PRIMARY KEY (id),
) TYPE=MyISAM;
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)
) TYPE=MyISAM;
CREATE TABLE product (
id int(10) unsigned NOT NULL auto_increment,
name varchar(150) NOT NULL default '',
PRIMARY KEY (id)
) TYPE=MyISAM;
A category has a column called site_id which is a key to another table
called Site (the content of which is irrelevant here) which determines which
'web-site' the category appears on (the database supports multiple front
ends for the same set of products).

I am wanting to select all products that are not visible at all on a given
site (site_id=x). 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.id is null || c.site_id!=?)

which is almost right but not quite. Can anyone help ?
Jul 20 '05 #1
3 2267
"sks"
<sk*@magnum55.remove.andreplacewith.themostpowerfu lhandgunintheworld.com>
wrote in message
CREATE TABLE categories (
id bigint(20) unsigned NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
site_id bigint(20) NOT NULL default '0',
PRIMARY KEY (id),
) TYPE=MyISAM;
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)
) TYPE=MyISAM;
CREATE TABLE product (
id int(10) unsigned NOT NULL auto_increment,
name varchar(150) NOT NULL default '',
PRIMARY KEY (id)
) TYPE=MyISAM; 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.id is null || c.site_id!=?)

which is almost right but not quite. Can anyone help ?


In SQL the || means concatenate. If you replace that with OR, does it work?
Just a guess, as I haven't tried it out actually.

As an aside, what if c.site_id is not NULL, but the site it points to does
not exist (say you didn't have a cascade delete or restrict in place)? Do
you need to check for this too?
Jul 20 '05 #2
sks

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:JL********************@bgtnsc04-news.ops.worldnet.att.net...
"sks"
<sk*@magnum55.remove.andreplacewith.themostpowerfu lhandgunintheworld.com>
wrote in message
CREATE TABLE categories (
id bigint(20) unsigned NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
site_id bigint(20) NOT NULL default '0',
PRIMARY KEY (id),
) TYPE=MyISAM;
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)
) TYPE=MyISAM;
CREATE TABLE product (
id int(10) unsigned NOT NULL auto_increment,
name varchar(150) NOT NULL default '',
PRIMARY KEY (id)
) TYPE=MyISAM;
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.id is null || c.site_id!=?)

which is almost right but not quite. Can anyone help ?


In SQL the || means concatenate. If you replace that with OR, does it

work? Just a guess, as I haven't tried it out actually.
I'll try it.
As an aside, what if c.site_id is not NULL, but the site it points to does
not exist (say you didn't have a cascade delete or restrict in place)? Do
you need to check for this too?


I need to check that site_id does not equal the value I specify. So other
site_id values and null should be treated together if you see what I mean.
Jul 20 '05 #3
Siemel Naran wrote:
In SQL the || means concatenate. If you replace that with OR, does it work?
Just a guess, as I haven't tried it out actually.


The ANSI/ISO SQL-92 standard defines || as a string concatenation
operator, but MySQL uses CONCAT(string1, string2, ...) for
concatenation. MySQL uses || as a synonym for OR.

This is an unfortunate departure from the standard. But in this case,
conforming to the standard has little value, because several other RDBMS
implementations fail to match the SQL standard as well. The bottom line
is that string concatenation expressions are highly unlikely to be
portable between RDBMS vendors.

Bill K.

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
3
by: Ian Boyd | last post by:
i know nothing about DB2, but i'm sure this must be possible. i'm trying to get a client to create a view (which it turns out is called a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but...
5
by: Dev | last post by:
Hello all, I need to do a left out join where a.field1 ilike %b.field2% But I can not figure out the exact syntax to using the ilike in the join? ----------
2
by: tricard | last post by:
Good day all, I have a large outer joined query that I want to have some criteria. The select query is gathering all part numbers from tblPartNumbers, left joining to tblPartNumberVendor (since...
1
by: Eitan M | last post by:
Hello, I want to do select like this : select t1.col_2 from table_1 t1, table_2 t2 where t1.col_1 = t2.col_1 (+) The above is correct syntax for Oracle. What is the correct syntax for...
9
by: shanevanle | last post by:
I have two tables that are pretty big. I need about 10 rows in the left table and the right table is filtered to 5 rows as well. It seems when I join the tables in the FROM clause, I have to...
5
by: Sascha.Moellering | last post by:
Hi, I receive the error code SQL0338N if I try to compile this statement (part of the statement): .... left outer join lateral (SELECT * FROM LIZSYSABA.VWZL0359TBS WHERE tbs_name = CASE WHEN...
3
by: pbassutti | last post by:
Hello, I'm trying to link two tables... one for Employees and the other for Timecards I need to get a list of employees that do not have timecards on an SPECIFIC DATE I tried the follonwing
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.