RE: sql join help

2004-04-11 Thread Matt Chatterley
Hmm.

SELECT o.*
FROM orders o
INNER JOIN orderitems oi ON o.orderid = oi.orderid
INNER JOIN products p ON p.productid = oi.productid
AND p.productparentid  2
WHERE o.orderstatus =2

Not sure why you are checking for a NULL ordered in orderitems? That would
suggest you get back only items that have no associated order?

The above should do what you state below, though - I think!


Cheers,

Matt

-Original Message-
From: Michael Collins [mailto:[EMAIL PROTECTED] 
Sent: 11 April 2004 05:14
To: [EMAIL PROTECTED]
Subject: sql join help

I suppose this would be easier with subselects but I am using MySQL 4:

I want all orders that are of orderStatus 2 and whose orderitems 
contain a product that is in a productparent category greater than 2. 
An orderitem can only have one product, and that product has a single 
certain product parent (defined in the products table). This is how 
the tables are related:

members - orders - orderitems - products - productparents

I have tried the following, but I know it is not correct:

SELECT count(*) FROM orders AS o
LEFT JOIN members AS m USING (memberId)
LEFT JOIN orderItems AS oi ON (o.orderId=oi.orderId)
LEFT JOIN products AS p ON (oi.productId=p.productId) AND 
(p.productParentId  2)
WHERE (oi.orderId IS NULL) AND (o.orderStatus=2);

-- 
Michael
__
||| Michael Collins
||| Kuwago Inc  mailto:[EMAIL PROTECTED]
||| Seattle, WA, USAhttp://michaelcollins.net

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]




-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



RE: sql join help

2004-04-11 Thread Michael Collins
Thanks Matt. But, what I am attempting to do with (oi.orderId IS 
NULL) is to eliminate the orders that have at least one order item 
with products of product parents not equal to 2. What you show finds 
all orders that have orderitems that have a product parent greater 
than 2, however, if that orderitem is in an order that also has a 
product parent of 1 or 2, I don't want to show that order. For 
example, I have these product Ids:

1 - hats - product parent 2 (clothes)
2 - shoes - product parent 2 (clothes)
3 - water pistols - product parent 3 (toys)
4 - rubber chickens - product parent 3 (toys)
And order ids:

222 has: 1 and 3 product Ids, o.orderstatus =2
333 has: 3 and 4 product Ids , o.orderstatus =2
What you propose would find both order id 222 and 333 since both 
contain a product Id greater than 2. What I want is to only find 
those orders that do not have 1 or 2, in other words just order id 
333 (from the two examples shown above).

Help would be greatly appreciated!

Michael

At 11:10 AM +0100 4/11/04, Matt Chatterley wrote:
Hmm.

SELECT o.*
FROM orders o
INNER JOIN orderitems oi ON o.orderid = oi.orderid
INNER JOIN products p ON p.productid = oi.productid
AND p.productparentid  2
WHERE o.orderstatus =2
Not sure why you are checking for a NULL ordered in orderitems? That would
suggest you get back only items that have no associated order?
The above should do what you state below, though - I think!

Cheers,

Matt

-Original Message-
From: Michael Collins [mailto:[EMAIL PROTECTED]
Sent: 11 April 2004 05:14
To: [EMAIL PROTECTED]
Subject: sql join help
I suppose this would be easier with subselects but I am using MySQL 4:

I want all orders that are of orderStatus 2 and whose orderitems
contain a product that is in a productparent category greater than 2.
An orderitem can only have one product, and that product has a single
certain product parent (defined in the products table). This is how
the tables are related:
members - orders - orderitems - products - productparents

I have tried the following, but I know it is not correct:

SELECT count(*) FROM orders AS o
LEFT JOIN members AS m USING (memberId)
LEFT JOIN orderItems AS oi ON (o.orderId=oi.orderId)
LEFT JOIN products AS p ON (oi.productId=p.productId) AND
(p.productParentId  2)
WHERE (oi.orderId IS NULL) AND (o.orderStatus=2);
--
Michael
__
||| Michael Collins
||| Kuwago Inc  mailto:[EMAIL PROTECTED]
||| Seattle, WA, USAhttp://michaelcollins.net
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]


--
Michael
__
||| Michael Collins
||| Kuwago Inc  mailto:[EMAIL PROTECTED]
||| Seattle, WA, USAhttp://michaelcollins.net
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]


Re: sql join help?

2003-02-19 Thread KH Chiu
Multiple table update is only supported from 4.04 or above. If you are using 
3.x, I think you should put the result into another table, delete MSI_List 
and rename the table to MSI_List.

I would also very interested to know whether there exist a more elegant 
solution for 3.x.

Best regards,

 sql
 
 I have a zip code db named MSI_Zipcodes that contains
 
 city
 state
 zipcode (index)
 
 I also have a db named MSI_List that contains
 
 ID (index)
 email
 city(empty)
 state(empty)
 zip
 
 My problem is:
 
 How can i bring the proper info from MSI_Zipcodes(city and state)
 and enter it (city and state) into the empty fields of MSI_List in 
 accordance with the zip code in MSI_List?
 
 Thanks in advance!
 
 Todd Clemmer
 
 -
 Before posting, please check:
http://www.mysql.com/manual.php   (the manual)
http://lists.mysql.com/   (the list archive)
 
 To request this thread, e-mail [EMAIL PROTECTED]
 To unsubscribe, e-mail mysql-unsubscribe-
[EMAIL PROTECTED]
 Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


--
Yours,
KH Chiu
CA Computer Consultants Ltd.
Tel: 3104 2070 Fax: 3010 0896
Email: [EMAIL PROTECTED]
Website: www.caconsultant.com


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: SQL join help

2002-05-20 Thread ds

On Mon, 2002-05-20 at 13:34, Ray Zimmerman wrote:
...
 For example, given the following data ...
 
CREATE TABLE Object (
  id int(11) NOT NULL auto_increment,
  PRIMARY KEY  (id)
);
 
INSERT INTO Object (id) VALUES (1);
INSERT INTO Object (id) VALUES (2);
INSERT INTO Object (id) VALUES (3);
INSERT INTO Object (id) VALUES (4);
INSERT INTO Object (id) VALUES (5);
INSERT INTO Object (id) VALUES (6);
 
CREATE TABLE Link (
  parent int(11) NOT NULL,
  child  int(11) NOT NULL,
);
 
INSERT INTO Link (parent, child) VALUES (1, 4);
INSERT INTO Link (parent, child) VALUES (1, 5);
INSERT INTO Link (parent, child) VALUES (2, 4);
INSERT INTO Link (parent, child) VALUES (2, 5);
INSERT INTO Link (parent, child) VALUES (2, 6);
INSERT INTO Link (parent, child) VALUES (3, 4);
 
 ... I want to find all Objects that have exactly 2 children with ids 
 4 and 5  (i.e. should match 1, but not 2 or 3) ... what's the query 
 syntax?

SELECT parent FROM Link WHERE child=4 OR child=5 
GROUP BY child HAVING count(*)=2;

 How about if I want to find all Objects which have no children (i.e. 
 should match 4, 5 and 6, but not 1, 2 or 3).

SELECT id FROM Object LEFT JOIN Link ON (Object.id=Link.parent) 
WHERE Link.parent IS NULL;

Hope this simple queries solves your problem.

-- 
dsoares
(sql)

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: SQL join help

2002-05-20 Thread Ray Zimmerman

I figured it out ...

At 3:22 PM +0100 5/20/02, ds wrote:
On Mon, 2002-05-20 at 13:34, Ray Zimmerman wrote:
...
   For example, given the following data ...

 CREATE TABLE Object (
   id int(11) NOT NULL auto_increment,
   PRIMARY KEY  (id)
 );

 INSERT INTO Object (id) VALUES (1);
 INSERT INTO Object (id) VALUES (2);
 INSERT INTO Object (id) VALUES (3);
 INSERT INTO Object (id) VALUES (4);
 INSERT INTO Object (id) VALUES (5);
 INSERT INTO Object (id) VALUES (6);

 CREATE TABLE Link (
   parent int(11) NOT NULL,
   child  int(11) NOT NULL,
 );

 INSERT INTO Link (parent, child) VALUES (1, 4);
 INSERT INTO Link (parent, child) VALUES (1, 5);
 INSERT INTO Link (parent, child) VALUES (2, 4);
 INSERT INTO Link (parent, child) VALUES (2, 5);
 INSERT INTO Link (parent, child) VALUES (2, 6);
 INSERT INTO Link (parent, child) VALUES (3, 4);

  ... I want to find all Objects that have exactly 2 children with ids
  4 and 5  (i.e. should match 1, but not 2 or 3) ... what's the query
  syntax?

SELECT parent FROM Link WHERE child=4 OR child=5
GROUP BY child HAVING count(*)=2;

Actually, while this may work for the specific example, it doesn't 
work in general ... here's the query I finally found to work ...

SELECT * FROM
 Object LEFT OUTER JOIN Link L0 ON
   (Object.id = L0.parent AND L0.child NOT IN (4,5)),
 Link L1,
 Link L2
   WHERE L1.parent = Object.id AND L1.child = 4
 AND L2.parent = Object.id AND L2.child = 5
 AND L0.id IS NULL;


   How about if I want to find all Objects which have no children (i.e.
  should match 4, 5 and 6, but not 1, 2 or 3).

SELECT id FROM Object LEFT JOIN Link ON (Object.id=Link.parent)
WHERE Link.parent IS NULL;

And yes, this is basically what I came up with here too. Thanks.

-- 
  Ray Zimmerman  / e-mail: [EMAIL PROTECTED] / 428-B Phillips Hall
   Sr Research  /   phone: (607) 255-9645  /  Cornell University
Associate  /  FAX: (815) 377-3932 /   Ithaca, NY  14853

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php