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
.
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.
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.
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.
Syntax: SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name(s) FROM table_name WHERE condition);
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.
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.
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])
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, ...
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:
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With