Check if a row exists or not in postgresql - postgresql

I have seen many posts about this in SO. But I could not get an answer.
I want to the query to check if a particular row exists or not in a table. If it exists, it should return me a string true and stop the search there itself and if not return false.

select
case when exists (select true from table_name where table_column=?)
then 'true'
else 'false'
end;
But it would be better to just return boolean instead of string:
select exists (select true from table_name where table_column=?);

Spoiler:
-- EXPLAIN ANALYZE
WITH magic AS (
WITH lousy AS ( SELECT * FROM one WHERE num = -1)
SELECT 'True'::text AS truth
WHERE EXISTS (SELECT * FROM lousy)
UNION ALL
SELECT 'False'::text AS truth
WHERE NOT EXISTS (SELECT * FROM lousy)
)
SELECT *
FROM magic
;

Related

Postgres execute function if exists

Is it possible to write a select statement that executes function if exists ?
SELECT COALESCE (CASE WHEN EXISTS (SELECT * FROM pg_proc WHERE proname = 'func_name') THEN null ELSE false END, (SELECT func_name() ))
OR
select 1
WHERE EXISTS (
select * from pg_proc where proname = 'func_name'
) AND EXISTS (
select func_name();
)
Above are ofcourse wrong statements but just to give you an idea. Is it possible?? I am using Postgres 10.1 so its not the latest
You can probably get away with using query_to_xml()
select query_to_xml('select func_name()', false, true, '')
where exists (select *
from pg_proc
where proname = 'func_name'
and pronamespace = 'public'::regnamespace)

Case-statment in postgresql for doing different things depending on if a column exist or not

I'm trying to do a Case-statment in postgres to do different things depending on if a column exist or not,
CASE
WHEN select exists (select * from information_schema.columns where
table_name = 'mytable' and column_name = 'mycolumnt')
THEN select mycolumn from mytable where mycolumnt = true
ELSE select mycolumn from mytable
END
After executing the case statement I got below error
ERROR: syntax error at or near "case"
Any tips what I'm doing wrong? My postgres-knowledge is pretty basic.
Your query should start with SELECT. Like:
SELECT mycolumn
FROM mytable
WHERE CASE WHEN exists (
select *
from information_schema.columns
where table_name = 'mytable' and column_name = 'mycolumnt')
THEN mycolumnt = true
ELSE true
END
Also, I would recommend you to always have mycolumnt, just set it to false when there is no translation. This would simplify your query to this:
SELECT mycolumn
FROM mytable
WHERE mycolumnt = true

Checking if string is in table

Hi I have table called mytable and 1 column first_name , how can I check if 'John' is in this table and return the result as true/false.
You can utilize a EXISTS-query:
select exists (select * from mytable where mytable.first_name = 'John')
The above query will return a boolean which will be true if the sub-query inside the braces returns any rows at all; the boolean will false if the sub-query return no rows.
SELECT count(*) FROM mytable WHERE first_name LIKE 'John';
returns number of occurrences. If there is no John in table, returns zero.
SELECT CASE WHEN COUNT(*) > 0 THEN 'true' ELSE 'false' END
FROM mytable
WHERE first_name LIKE '%John%'

Return a value if select returned null

I need to return a value if select returned null. however I found a solution here by putting a query in a sub-query
SELECT COALESCE((SELECT id FROM tbl WHERE id = 9823474), 4) AS id FROM RDB$DATABASE;
The query above would return Null because the value 9823474 does not exist in the table but I want to return a value in that case (for ex 4) so I found the only solution to use select inside sub query and then COALESCE would work, If I did not do that COALESCE will also return Null.
Is it the only solution ?
No, that is not an only way for example
Select first 1 id from (
Select id FROM tbl WHERE id = 9823474
Union All
Select 4 from rdb$database)
Or you can use anonymous procedure http://firebirdsql.su/doku.php?id=execute_block
EXECUTE BLOCK RETURNS ( id integer )
AS
BEGIN
IF ( EXISTS (SELECT * FROM tbl WHERE id = 9823474) )
THEN id = 9823474;
ELSE id = 4;
SUSPEND;
END
... there always are many methods there

Select records but if null, show a message

I know this easy, just can't get the syntax right. If no results, show a message instead:
select COALESCE(select SomeField from SomeTable where [#SomeTableVariable].SomeID = #SomeID, 'none to delete')
if that query is only going to return one row then:
select COALESCE(
(select SomeField from SomeTable where [#SomeTableVariable].SomeID = #SomeID),
'none to delete'
)
should do it.