Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in how to evaluate multiple rows as one using IF( )

Status
Not open for further replies.

muzik

MIS
Jul 29, 2003
1
US
Need help wit this problem:


Each record for a PhysicalSource has a Status number.

I need to do a SELECT statement to find out if there is a Status = 2 in any of the records.

If Status = 2 then I need to return all rows for this PhysicalSource else if Status != 2 then I need to exit the query (don’t return anything).


The statement is as follows:


SELECT SourceID, OriginalTime, State FROM sources WHERE SourceID = ? AND PhysicalSource = ? AND IF(Status = 2, 0, 1) Order by SourceID ASC



As I found out the “IF(Status = 2, 0, 1)” evaluates every row and returns all others with Status !=2.


The options are limited, I inherited a GUI (can’t modify it) that must be use and only accepts one query at a time using MySQL version 4.1.11.

Could someone help with a way to have this evaluate multiple rows, as one, and if in any of them Status = 2 then exit the query?

Thanks
 
How about:
[tt]
SELECT SourceID, OriginalTime, State
FROM sources
WHERE
SourceID = ?
AND PhysicalSource = ?
AND
EXISTS
(
SELECT 1
FROM sources
WHERE
sourceid= ?
AND physicalsource = ?
AND Status = 2
LIMIT 1
)
ORDER BY SourceID ASC
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top