Solved

SQL Select statement

Posted on 2011-02-11
13
408 Views
Last Modified: 2012-05-11
Hi,

I have a table (TABLE_1)

I need a single select statement, to return matched records.


TABLE_1
-------------------------
ID      NAME      URL
1      A      A.COM
1      B      B.COM
NULL      A      A1.COM
NULL      B      B1.COM
NULL      C      C1.COM


For example

SQL: SELECT * FROM TABLE_1  WHERE ID = 1
Return
----------------------------------------------
ID      NAME            URL
1      A            A.COM
1      B            B.COM



SQL: SELECT * FROM TABLE_1  WHERE ID = 2

OR

SQL: SELECT * FROM TABLE_1  WHERE ID = 100

BOTH SELECT SHOULD RETURN (ID=2 is not in table_1)
----------------------------------------
ID      NAME            URL
NULL      A            A1.COM
NULL      B            B1.COM
NULL      C            C1.COM
0
Comment
Question by:gdupadhyay
  • 4
  • 2
  • 2
  • +3
13 Comments
 
LVL 14

Expert Comment

by:leoahmad
Comment Utility
may be this (not understand your question)

SELECT * FROM TABLE_1  WHERE ID != 1
0
 
LVL 22

Expert Comment

by:Matt V
Comment Utility
Are you trying to select IDs with no ID assigned?

how about

SELECT * FROM TABLE_1 WHERE ID = NULL;

or

SELECT * FROM TABLE_1 WHERE !ID;

0
 
LVL 9

Author Comment

by:gdupadhyay
Comment Utility
It should return matched records otherwise ID=NULL records

If ID=1 (ID=1 is in table)
should return matched records

If ID = 3
should return only ID = NULL records, because ID=3 is not in table_1

I hope you understand my question
0
 
LVL 10

Expert Comment

by:GlobaLevel
Comment Utility
--select all id records from both tables  where id = 1 for both both tables...
Select * from table_1 t1
JOIN Table2 t2 ON t1.id = t2.id
where t1.id = '1'
0
 
LVL 14

Expert Comment

by:leoahmad
Comment Utility
in dbms Null means unavailable, unassigned, unknown so it should not equal to 3 or 4 or 100 or something else other than Null.

if your query returns no rows when ID = 3, that's according to said rules.
0
 
LVL 9

Author Comment

by:gdupadhyay
Comment Utility
Please read my question. Its not a simple select statement.  I have only 1 table
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
then you need this statement

return rows with id=? or if id=? doesn't exist then return rows with an id of null


select x.* from table_1 as x
where x.id=?
 or (x.id is null
              and not  exists (select id from table_1 as y where y.id=?)
     )
order by 1,2
0
 
LVL 22

Assisted Solution

by:Matt V
Matt V earned 100 total points
Comment Utility
What you have is a programming logic question, not a query question.

You need your application to do a query, and if nothing is returned then re-issue the query to pull all NULL id records.
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 150 total points
Comment Utility
Mattymotas is correct ...

and it would depend on the frequency with which you assume that a not present id will be asked for as to whether it would be better to have the combined statement or two statements with a logic test between ...

either way the SQL code should go in a stored procedure and be executed within one...
that way your application still only makes one request to the database server,,,

but the optimal access solution can be (hopefully) provide in one call sequence.
0
 
LVL 10

Expert Comment

by:GlobaLevel
Comment Utility
alos..where is this going...is it for a web app...I would as much on the client side...in  order to limit db calls...so for example if the id is somthing..then make the call to the db...that way if you have many users...you can filter them out on the client and save on resources on the db side...but I found your question to be very confusing...
0
 
LVL 9

Author Comment

by:gdupadhyay
Comment Utility
Thanks for response.

I need to write stored procedure, what is the best way to write a stored procedure to fulfill above requirement?
0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
Comment Utility
Use Lowfatspread's query and create a proc.
create procedure sp_Proc 
@var int as  
select x.* from table_1 as x
where x.id=@var
 or (x.id is null 
              and not  exists (select id from table_1 as y where y.id=@var)
     )
order by 1,2

Open in new window

Execute the proc by passing the variable.
exec sp_Proc @var = 100

Open in new window

0
 
LVL 9

Author Closing Comment

by:gdupadhyay
Comment Utility
Thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now