Connecting Tech Pros Worldwide Forums | Help | Site Map

How to group boolean expressions in a WHERE clause

Member
 
Join Date: May 2007
Posts: 102
#1: Jun 24 '07
Expand|Select|Wrap|Line Numbers
  1. select * from cbomnew 
  2. where PartNumber not in 
  3.   (select PartNumber from cbom_filtered)
  4. and comCode= 'PCEC' || comCode='LOX' || comCode='CCEC' 
  5.  
i wan to select those partnumber which not in cbom_filtered.. but why the partnumber which exist in cbom_filtered still appear...?? my code correct???

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: Jun 24 '07

re: How to group boolean expressions in a WHERE clause


Because you need to group your boolean expressions together, or else it will evaluate them seperateley and return any row that matches any of them.

This is how your query should look:
Expand|Select|Wrap|Line Numbers
  1. select * from cbomnew 
  2. where PartNumber not in 
  3.   (select PartNumber from cbom_filtered)
  4. and  
  5. (    comCode= 'PCEC'
  6.   OR comCode='LOX'
  7.   OR comCode='CCEC' 
  8. )
  9.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#3: Jun 24 '07

re: How to group boolean expressions in a WHERE clause


I have edited the thread's title to better describe it's contents.
Please read the Posting Guidlines before posting

Moderator
Member
 
Join Date: May 2007
Posts: 102
#4: Jun 24 '07

re: How to group boolean expressions in a WHERE clause


Pls advice me...

i have PartNumber and SupplierName as my primary key..

[PHP]"select * from cbomnew where PartNumber not in (select PartNumber from cbom_filtered) and (comCode= 'PCEC'or comCode='LOX'or comCode='CCEC' )"[/PHP]

how do i put like

select PartNumber and SupplierName not in( select PartNumber from cbom_filtered) ?????

i wan to compare 2 data which are partnumber and suppliername.. how do i do that...
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Jun 24 '07

re: How to group boolean expressions in a WHERE clause


Heya, lyealain.

Quote:

Originally Posted by lyealain

select PartNumber and SupplierName not in( select PartNumber from cbom_filtered) ?????

You'll want to use WHERE NOT EXISTS
Member
 
Join Date: May 2007
Posts: 102
#6: Jun 25 '07

re: How to group boolean expressions in a WHERE clause


[PHP] SELECT * FROM cbomnew WHERE NOT EXISTS (
SELECT * FROM cbom_filtered
WHERE cbomnew.partnumber = cbom_filtered.partnumber
AND[/b] cbomnew.suppliername = cbom_filtered.suppliername)
where (comCode= 'PCEC' or comCode='LOX' or comCode='CCEC' );[/PHP]

the above code give me error!!!!

i wan to select * where (comCode= 'PCEC' or comCode='LOX' or comCode='CCEC' ) and with these 2 constraints

[PHP]WHERE cbomnew.partnumber = cbom_filtered.partnumber
AND cbomnew.suppliername = cbom_filtered.suppliername)[/PHP]
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#7: Jun 25 '07

re: How to group boolean expressions in a WHERE clause


Heya, lyealain.

You have an errant '[/b]' in your SQL query. Is this intentional?
Member
 
Join Date: May 2007
Posts: 102
#8: Jun 25 '07

re: How to group boolean expressions in a WHERE clause


ohh.. sorry.. ignore that [/b]..
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#9: Jun 25 '07

re: How to group boolean expressions in a WHERE clause


Heya, lyealain.

What error does MySQL give you when you run that query?
Member
 
Join Date: May 2007
Posts: 102
#10: Jul 7 '07

re: How to group boolean expressions in a WHERE clause


hi.. very sorry for late reply...

i have updated my code to be like this...
[PHP]
dim qryFilter, rsReport
qryFilter= "select * from cbomnew where PartNumber + SupplierName not in (select PartNumber + SupplierName from cbom_filtered)and (RFQ='"&rfq&"')and (DateUploaded='"&lastup&"') and comCode IN (Select Distinct ComCode From comcode_table) "
[/PHP]
my idea is if the above not eof... then insert into cbom_filtered

but the above sentence keep inserting the same data into my cbom_filtered..

i have select something where not in the other table.. i wan to compare both PartNumber + SupplierName with cbom_filtered

cbomnew table: contains
partnumber: 123 SupplierName = angel
partnumber: 456 SupplierName = holy

in cbom_filtered table: contains
partnumber: 123 SupplierName = devil
partnumber: 456 SupplierName = ghost

if in cbom_filtered table: contains
partnumber: 123 SupplierName = angel
then don insert..
else .. insert..
Member
 
Join Date: May 2007
Posts: 102
#11: Jul 8 '07

re: How to group boolean expressions in a WHERE clause


help help... experts pls help...thanks thanks thanks..hahahhaha
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#12: Jul 8 '07

re: How to group boolean expressions in a WHERE clause


Try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `cbomnew` WHERE NOT EXISTS(SELECT * FROM `cbom_filtered ` WHERE (`cbom_filtered`. `PartNumber ` = `cbomnew `.`PartNumber`) OR (`cbom_filtered`. `SupplierName ` = `cbomnew `.`SupplierName`)) AND (`RFQ` = '" & rfq & "') AND (`DateUploaded` = '" & lastup & "') AND EXISTS(SELECT * FROM `comcode_table` WHERE `comcode_table`.`ComCode` = `cbomnew`.`ComCode`)
Make sure your `cbomnew`, `cbom_filtered` and `comcode_table` tables are indexed on `PartNumber`, `SupplierName`, `RFQ`, `DateUploaded` and `ComCode`.

http://dev.mysql.com/doc/refman/5.0/...ubqueries.html
Member
 
Join Date: May 2007
Posts: 102
#13: Jul 9 '07

re: How to group boolean expressions in a WHERE clause


[PHP]WHERE (`cbom_filtered`. `PartNumber ` = `cbomnew `.`PartNumber`) OR (`cbom_filtered`. `SupplierName ` = `cbomnew `.`SupplierName`)
[/PHP]
thanks.. should i put 'or' / 'and' for the above statement that u specified..

i think is AND??? because i wan to include both partnumber and suppliername to compare with other table at the same time
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#14: Jul 9 '07

re: How to group boolean expressions in a WHERE clause


Heya, lyealain.

Quote:

Originally Posted by lyealain

i think is AND??? because i wan to include both partnumber and suppliername to compare with other table at the same time

The query I posted will return a result if either one exists. If you only want to return results that have both values, then you are correct; you'll want to change that to AND.
Reply