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
Related
Table Name: Worker,
Fields : worker_id | first_name | last_name | department
I have a table name worker and i wanted to write an SQL query that fetches the unique values of DEPARTMENT from Worker table and prints its length. So i tried running this : (Database- Mysql)
select length(distinct(department)) from worker;
But it is giving an error saying:
ERROR 1064 (42000): 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 'distinct(department)) from worker' at line 1
But when i ran below query, it works perfectly fine:
select distinct(length(department)) from worker;
Can somebody please explain me why interchanging distinct and length function makes query works?
Thanks in advance!
Try not to use distinct like function but clause otherwise it will give syntax error.
Below sql statement will execute as shown below:
select distinct (length('xyz')) ---- length('xyz') : 3
select distinct (3) ---- output : 3
Distinct is not properly a function but a clause
select distinct length(department) from worker;
Anyway in MySQL work also with function syntax
select distinct( length(department)) from worker;
The code with the exchanged token don't work because DISTINCT produce an aggregated result removing the duplicated values,this implies that the outer length() function work on not correct set of rows or better the db engine see that there an improper use of the DISTINCT clause and raise the syntax error
select length( distinct 'A' ) this raise an error
If you want use the outer length() function you should code this way
select length(my_col) from (
select distinct department my_col from worker
) ;
correct answer :
select distinct <column_dept> as department***,*** (len(<column_dept>) as length_column_dept from xyz_table
select distinct( length(department)),department from worker group by department;
select distinct (Department) as 'Unique department', len(Department) as 'length of name' from Worker;
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
I am getting a 1064 error with the following statement - note: both clauses of the statement work OK individually. I understood that should any records be produced from the first clause then the second would be actioned? I don't want to use a stored procedure and techniques using a SET #val also seem to fail. Any ideas anyone please?
SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = 'ft_form_103' WHERE EXISTS (SELECT * FROM 'ft_form_103');
I could not understand your what you actually wanted, but looking at your script their is a syntax error. I have corrected the syntax and below sql will work. Hope this is what you were looking for.
SQL:
SELECT IFNULL((SELECT TABLE_NAME FROM information_schema.TABLES
WHERE TABLE_NAME = 'ft_form_103'),'Table not found') AS table_name;;
I want to select all columns where vaules of coulmns not equal to 'na'.
Thi is the query I use for this.
SELECT * FROM `wp_skilllist` WHERE column IS NOT NULL AND TRIM(column) <> 'na'
this is the error I get
MySQL server version for the right syntax to use near 'column IS NOT NULL AND TRIM(column) <> '' LIMIT 0, 30' at line 1
Can anybody help me to correct the query.
column is a reserved word, it should be between backticks to make it work (or better, use something else for your column name):
SELECT * FROM `wp_skilllist` WHERE `column` IS NOT NULL AND TRIM(`column`) <> 'na'
column is a keyword for mysql so avoid using it for your column name.
try this:-
SELECT *
FROM `wp_skilllist`
WHERE `column` IS NOT NULL
AND TRIM(`column`) != 'na';
please see the demo here
HERE SQLFIDDLE
Ok I've seen many similar questions but crawling over the answers couldn't make my trigger error free!
Result I need is: Whenever a new value is inserted in the database table temp_pool, it triggers and if the new address is not equal to the previous address value with the same dev_id as that of this NEW.dev_id insert the new values to location table.
Here is the query (sample):
CREATE TRIGGER filter
after insert on geo.temp_pool
for each row
BEGIN
DECLARE OLD_ADDR VARCHAR(2048);
OLD_AADR = select address from temp_pool where dev_id like NEW.dev_id
order by date desc, time desc limit 1;
IF (OLD_ADDR != NEW.address) THEN
INSERT INTO a3380361_geo.location
VALUES (NEW.dev_id,NEW.address,NEW.lat,NEW.lng,NEW.date,NEW.time);
END IF;
END
$$
I am using the phpMyAdmin editor and set the delimiter to $$.
The error that I am getting is:
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 '= select address from temp_pool where 1 order by date desc, time desc limit 1; ' at line 5
I strongly believe that there is some problem with assigning values from SELECT to a variable [OLD_ADDR], so is there any way to solve my issue?
The logic is simple and the requirement is understandable from the query, right?
Open to all opinions and suggestions.
Instead of:
OLD_AADR = select address from temp_pool where dev_id like NEW.dev_id
Use:
SET OLD_AADR = (select address
from temp_pool
where dev_id like NEW.dev_id
order by address
limit 1);
Or using the non-standard SELECT assignment statement(not sure whether mysql supports it or not):
SELECT OLD_AADR = address from temp_pool where dev_id like NEW.dev_id
order by address
limit 1;
Not that in both cases the SELECT statement has to return only a scalar value. Thats why I used LIMIT 1.
Did you notice the typo?
OLD_ADDR OLD_AADR