Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return 1 if number exists in table and 0 otherwise

I want to return 1 if some number already exists in table and 0 otherwise.

I tried something but it doesn't work:

select
case when 100 in (select distinct id from test) then '1'
else '0'
from test

I want something similar to exists function that already exists in PostgreSQL, but instead of true and false I want 1 or 0.

like image 975
thecoparyew Avatar asked Nov 20 '14 17:11

thecoparyew


People also ask

How do you use exists with if in SQL?

The SQL EXISTS OperatorThe EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns TRUE if the subquery returns one or more records.

What do 0 and 1 mean in SQL?

A query like this can be used to ping the database. The clause: WHERE 1=0. Ensures that non data is sent back, so no CPU charge, no Network traffic or other resource consumption. A query like that can test for: server availability.

How do you know if data exists in one table and not in another?

To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.

What is the correct syntax for if exists expression?

Syntax: SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name(s) FROM table_name WHERE condition);

How to return a value if a given value exists in Excel?

Please apply the following formula to return a value if a given value exists in a certain range in Excel. 1. Select a blank cell, enter formula =VLOOKUP(E2,A2:C8,3, TRUE) into the Formula Bar and then press the Enter key.

How do you check if an if function returns yes or no?

We check this using MATCH and IF functions in Excel, such as This formula uses the MATCH function as a logical condition and If the MATCH function returns relative position of a value, then the IF function returns “YES,” otherwise this formula returns #NA error as shown below.

How to check if a value exists in a column using match?

Check if a value exists in a column using MATCH. Excel’s MATCH function searches for a value in a column or array and returns its relative position based on your chosen match type, whether exact or partial match. If the value is not found, then it returns a #NA error. Its syntax is: MATCH (value, array, [match_type])

How to check if a certain invoice exists in a column?

We want to check if a certain invoice exists in that column, and return “YES,” otherwise return #NA. We check this using MATCH and IF functions in Excel, such as. =IF (MATCH (D3,$A$2:$A$17,0),"Yes") This formula uses the MATCH function as a logical condition and If the MATCH function returns relative position of a value, ...


2 Answers

EXISTS yields a boolean result.
The simple way to achieve what you are asking for is to cast the result to integer:

SELECT (EXISTS (SELECT FROM test WHERE id = 100))::int;

TRUE is 1.
FALSE is 0.

Or with UNION ALL / LIMIT 1 (probably slightly faster):

SELECT 1 FROM test WHERE id = 100
UNION  ALL
SELECT 0
LIMIT  1;

If a row is found, 1 is returned and Postgres stops execution due to LIMIT 1.
Else, 0 is returned.

See:

  • Return a value if no record is found
like image 122
Erwin Brandstetter Avatar answered Sep 21 '22 16:09

Erwin Brandstetter


If the field you are testing is the Primary Key (or some other unique constraint), then you can simply return the count (which will always be 0 or 1):

SELECT count(*) FROM test WHERE id = 100;
like image 35
Sam Choukri Avatar answered Sep 24 '22 16:09

Sam Choukri