Sql Exists vs MySql Exists - mysql

In MySql I can return (what is effectively) a boolean using this to determine whether a database exists by name.
SELECT EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'MyDatabase')
What is the same in MS SQL?

In SQL Server you can do it this way:
IF EXISTS (SELECT 1 FROM ...)
SELECT 1
ELSE
SELECT 0
It's interesting that although the EXISTS function does return a boolean that can be tested by an IF, it can't be selected directly.
This doesn't work in SQL Server:
SELECT EXISTS(SELECT 1)
But this does:
IF EXISTS(SELECT 1)
SELECT 1
ELSE
SELECT 0
Weird.
EDIT: On further reflection, I guess MySQL might treat EXISTS() like a function that returns a value, whereas SQL Server treats it as a conditional expression that either passes or fails but doesn't return a value.
So in SQL Server, trying to SELECT the result of an EXISTS() is like trying to SELECT ('a' > 'b'). It can't do it. I wonder what MySQL does if you try?

There is no such thing as a boolean in t-sql. The closest is a bit which actually allows three values (0, 1 and NULL). If you want to select either 1 or 0 you can do this by converting count(*) to a bit. Any value other than 0 will be a 1.
select MyResult = convert(bit, count(*))
FROM information_schema.schemata
where SCHEMA_NAME = 'YourSchemaName'

Use CASE WHEN EXISTS()..
SELECT CASE WHEN EXISTS(SELECT * FROM ...) THEN 1 ELSE 0 END

Related

MySQL If variable equals then execute query

I have a requirement in MySQL 5.7 to only run a query if a condition is true.
For example, below we have a variable called x. If it equals 8, we're OK to run the query.
I'm trying to use an IF statement for this
Can anyone tell me what's going wrong?
Thanks
SET #x = (select count(*) from (select distinct tbl from db.tbl where dt = CURDATE())x);
IF #x = 8
BEGIN
SELECT * from db.tbl1
END
You don't need a procedure. Plain SQL will do:
SELECT * from db.tbl1
WHERE (select count(distinct col) from db.tbl where dt = CURDATE()) = 8
Note also the simplification using count(distinct ...)

Return yes/no from select, no flow control or adding tables to the database

I'm running a select statement checking for the existence of output froma sub-query. Its current form is the following.
select exists(subquery)
Now, the parameters of the solution are:
The output must be a single row, with a single entry, either 'Yes' or "no
Any form of flow control is disallowed
Adding a table to the database is disallowed (for example to 'translate' 1 to Yes and 0 to No through a query)
I'm kind of at a loss. My best idea is to produce the '1-yes, 0-no' 'table' via subquery, but I haven't the slightest how that can be done.
Well, it's not pretty, but you can cobble a look up table together with SELECT and UNION in a sub-query, then join to that from your EXISTS query.
Set up a really lame table:
create table tbl (value integer);
insert into tbl
select 1;
Then the query:
select
a.answer
from
(select exists(select 1 from tbl where value = 3) as value) as e
join
(select 1 as value, 'yes' as answer
union
select 0, 'no'
) as a
on
a.value = e.value
In this case, the table tbl only has one row and one column, which contains a 1, so the EXISTS returns a 0 and the outer query returns a no.
Here's a SQL Fiddle: http://www.sqlfiddle.com/#!9/a4f9ef/2/0
You seem to want a case expression:
select (case when exists(subquery) then 'yes' else 'no' end)
Personally, though, I prefer your version which returns 0 or 1 instead of 'no' or 'yes'.

Case in mysql to execute another query based on condition

I have a problem, the concept is value from #count to perform if and execute the query. What should I do to fix it?
Here is my code:
SELECT
CASE WHEN ((SELECT COUNT(*) FROM tb_dana_user WHERE userid=1234 AND password=1234) = 0)
THEN (SELECT * FROM tb_dana_user)
ELSE (SELECT 'bar')
END
When I change SELECT * FROM tb_dana_user to SELECT 'its true', it's working properly. How to fix this case? Sorry for bad english .the concept is when its true then the query will execute SELECT * FROM tb_dana_user
The CASE expression evaluates to a value, i.e. it is used to evaluate to one of a set of results, based on some condition.
Example:
SELECT CASE
WHEN type = 1 THEN 'foo'
WHEN type = 2 THEN 'bar'
ELSE 'foo-bar'
END AS column_name
FROM sometable`
The CASE statement executes one of a set of statements returns one result, based on some condition.
then statement has a query(select * from tb_dana_user) which returns multiple rows.
In your case either you can limit the value returned for select * from tb_dana_user to one at the same time column also one.
SELECT
CASE WHEN ((SELECT COUNT(*) FROM tb_dana_user WHERE userid=1234 AND password=1234) = 0)
THEN (SELECT id FROM tb_dana_user limit 1)
ELSE (SELECT 'bar')
END

Case with result set in mysql

I want's to test following query in mysql
SELECT result.* FROM
(CASE WHEN (2 = 2)
THEN
(SELECT * FROM mytable WHERE myID =2814 )
END) result ;
but it is sending syntax error.
Any idea what's wrong with it ?
you can do it in a round about way like so
SET #query := NULL;
SELECT #query := t.qry
FROM
( SELECT
CASE WHEN 2 = 2
THEN "SELECT * FROM mytable WHERE myID =2814"
WHEN another_condition
THEN "another_select"
END as qry
) t ;
PREPARE query1 FROM #query;
EXECUTE query1;
This is too long for a comment.
Obviously, your query is equivalent to:
SELECT *
FROM mytable
WHERE myID = 2814;
This makes it a bit hard to figure out what you are asking.
Fundamentally, though case is an expression, and the from clause does not accept expressions. It accepts table names, view names, subqueries, and the like. Second, the case statement has a few other problems.
It returns a scalar scalar value. The subquery could return more than one row.
Returning more than one column is not allowed
All the clauses need to return the same thing
If you wanted to select from multiple tables -- and all of them have the same structure -- you can use a union all construct:
select t.*
from table2 t
where . . .
union all
select t.*
from table2 t
where . . .
I would prefer to use a different query each time (which at least will allow mysql some chance of using indexes properly).
However assuming there are several sub queries that you want as a source for data depending on the check then maybe something like this:-
SELECT result.*
FROM
(
SELECT * FROM mytable WHERE 2 = 2 AND myID = 2814
UNION
SELECT * FROM mytable2 WHERE 1 = 2 AND myID = 2814
UNION
SELECT * FROM mytable3 WHERE 3 = 2 AND myID = 2814
) result ;

Does MySQL support "IF EXISTS"?

I think I have a conflict between my knowledge on SQL Server and MySQL.
When I run this query, I get an error always from MySQL:
If EXISTS (select * from tbl_admin) THEN
select 'OK';
END IF;
The error message is:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if EXISTS (select * from tbl_admin) then select '1'
-- select '1' WHERE EXISTS ' at line 1
Please help me and tell me am I wrong in writing this query? What's wrong?
I want to do something if I have something in tbl_admin table.
select 'ok'
from INFORMATION_SCHEMA.tables
where table_name = 'tbl_admin'
edit
To check if a table contains data you can do this:
SELECT 'OK' FROM dual
WHERE EXISTS (SELECT * FROM tbl_admin);
If I understand correctly, you know there is a table, you just need an info if there are any rows?
In that case I think this solves your problem:
SELECT
'OK'
FROM
Korisnik
WHERE
EXISTS( SELECT
COUNT(*)
FROM
Korisnik)
LIMIT 1;
You can use IF EXISTS to check for stored procedure or trigger existence. In SELECT queries you can use WHERE EXISTS or WHERE NOT EXISTS
http://dev.mysql.com/doc/refman/5.5/en/exists-and-not-exists-subqueries.html
You can do something like:
if ( (select count(*) from tbl_admin) > 0) then
...
This counts all the rows in the table. If no rows are there, it will return 0.
select case when count(*) > 0 then 'OK' else 'Empty' end from tbl_admin;
OR
select 'OK' from tbl_admin having count(*) > 0;
if you want to check table existence then use this
select 'Message' from INFORMATION_SCHEMA.tables where table_name = 'tbl_admin'
because all information is stored here.EXISTS also works fine in mysql.
select if(count(*), 'OK', '') as result from table_name where 1
This will print "OK" if there are records present, else nothing will be shown.
Use the normal select query..
Select 'OK' from table