473,398 Members | 2,403 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,398 software developers and data experts.

Not In / Not Exists

2
I'm attempting to find all records which are not in the results of a subquery; for example:

Expand|Select|Wrap|Line Numbers
  1. SELECT     *
  2. FROM         tblnews
  3. WHERE     (news_catID = 1) AND (NOT EXISTS
  4.                           (SELECT     TOP 5 *
  5.                             FROM          [tblnews]
  6.                             WHERE      news_catID = 1
  7.                             ORDER BY [news_pubdte] DESC, [news_priority] ASC))
  8. ORDER BY news_pubdte DESC, news_priority
The idea here is to display all records which do not show up as a result of the query:

Expand|Select|Wrap|Line Numbers
  1. SELECT     TOP 5 *
  2.                             FROM          [tblnews]
  3.                             WHERE      news_catID = 1
  4.                             ORDER BY [news_pubdte] DESC, [news_priority] ASC)
and have a news_catID of 1. The only problem is that the first query shown returns no records. I've also tried:

Expand|Select|Wrap|Line Numbers
  1. SELECT     *
  2. FROM         tblnews
  3. WHERE     (newsID NOT in
  4.                           (SELECT     TOP 5 *
  5.                             FROM          [tblnews]
  6.                             WHERE      news_catID = 1
  7.                             ORDER BY [news_pubdte] DESC, [news_priority] ASC))
But this returns the error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

I think that there needs to be a combination of the two, but am unsure about how to go about it. Any help for a hopeless newbie would be appreciated.
Sep 30 '06 #1
1 9618
hafa
2
OK, figured it out...

Expand|Select|Wrap|Line Numbers
  1. SELECT     *
  2. FROM         tblnews
  3. WHERE     (news_catID = 1) AND (newsID NOT IN
  4.                           (SELECT     TOP 5 newsID
  5.                             FROM          tblnews
  6.                             WHERE      news_catID = 1
  7.                             ORDER BY news_pubdte DESC, news_priority ASC))
  8. ORDER BY news_pubdte DESC, news_priority
Helps to declare a primary key in the inner query...
Sep 30 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: lawrence | last post by:
Google can't find me a good example of how to use the "if exists" syntax in MySql. Is it right that to use it this way: INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk") I want...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
8
by: Trev | last post by:
Which is more efficient: Select * from table1 where id in (select id from table2) or Select * from table1 where exists(select * from table2 where table2.id=table1.id)
15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
18
by: Dan | last post by:
I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My...
2
by: Zeno Lee | last post by:
I'm using File.Exists to test a file on my C: drive. My program was strongly named and had caspol -af run on it to allow it to run from the network. There are 3 ways I am doing this: 1) Run...
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
4
by: DEWright_CA | last post by:
I am trying to see if a file exists in a virtual directory, and if so run a method. I try doing File.Exists and the method runs but the file isn't there. Is there a web version of File.Exists or...
4
by: qwedster | last post by:
Howdy folks! I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries. However how to check if a value (that is...
4
by: ndedhia1 | last post by:
Hi. I am writing a java program in which I want to ftp a file to another unix box. First I have to check if the directory exists in which I am ftping into and if it does not exist, I have to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.