I get error for accessing all mysql columns - mysql

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

Related

MYSQL Creating table as select with xase statement converting field to numbered value

I have the following MYSQL query which creates table as select statement but having a case statment to change field formats to numbered fields to compare:
CREATE TABLE wm.bochg
AS
SELECT
wca.bochg.BochgID,
wca.bochg.SecID,
wca.bochg.OldOutValue,
wca.bochg.NewOutValue,
case when NewOutValue+0.0 >= OldOutValue+0.0 then NewOutValue ELSE '' END AS test2
from wca.bochg
WHERE
wca.bochg.SecID in (select secid from client.pfisin where accid = 416)
AND (wca.bochg.BochgID is null or wca.bochg.BochgID =
(select subbochg.BochgID from wca.bochg as subbochg
where subbochg.secid=bochg.secid
AND subbochg.Actflag <> 'D'
AND subbochg.NewOutValue+0.0 >= subbochg.OldOutValue+0.0
order by subbochg.BochgID desc limit 1)
or wca.bochg.BochgID is NULL);
However I am getting warnings when this is created saying:
'Truncated incorrect DOUBLE value: '' '
This is due to my case statment and the and clause within my subquery. Is there a way to cater for this issue and get rid of the warning message above going forward? Thanks
You are mixing datatypes in the case statement. Use something else than a string '' for the ELSE part (NULL, 0 or anything that suits your purpose).

MySQL: How to use CASE for ORDER BY clause

I have a stored procedure that must return a table after filtering rows based on inputs. Two of the inputs are sort_column and sort_dir. The query must ORDER BY sort_column in the sort_dir direction(ASC or DESC).
I have tried the following queries but in vain. The queries below have been simplified to only contain the relevant clauses. The other filters work correctly with no issues.
SELECT * FROM table ORDER BY sort_column sort_dir
SELECT * FROM table ORDER BY CASE sort_column
WHEN 'col1' THEN col1_name
WHEN 'col2' THEN col2_name END
CASE sort_dir WHEN 'asc' THEN ASC
ELSE DESC END
I concatenated the 2 inputs to 1 in the format _ and tried this:
SELECT * FROM table ORDER BY CASE sort_input
WHEN 'col1_asc' THEN col1_name ASC
WHEN 'col1_desc' THEN col1_name DESC
WHEN 'col2_asc' THEN col2_name ASC
WHEN 'col2_desc' THEN col2_name DESC END
I always get error #1064. It is different in each of the above cases but always points to the 'CASE' part. This is the error for option number 2 mentioned above
##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 'WHEN 'col1' THEN col1_name END CASE 'asc' WHEN 'desc' THEN DESC ELSE ' at line 4
The problem doesn't seem to be the column name. It is the sort direction that isn't working. If I try each of the above options without the 'ASC' and 'DESC' parts, there is no problem.
Am I doing anything wrong here?
Is there a better way to go about this apart from CASE?
MySQL version: 5.6
The best approach is multiple cases:
ORDER BY (CASE WHEN sort_input = 'col1_asc' THEN col1_name END) ASC,
(CASE WHEN sort_input = 'col1_desc' THEN col1_name END) DESC,
(CASE WHEN sort_input = 'col2_asc' THEN col2_name END) ASC,
(CASE WHEN sort_input = 'col2_desc' THEN col2_name END) DESC,
This may seem verbose. But, remember that CASE is an expression that returns a single value. Hence you cannot include ASC and DESC as part of the THEN.
Also important is the issue of data types. The SQL compiler decides on a single type for CASE expression. This can cause unexpected issues when the columns have different types.
The simplest solution is just to use multiple CASE expressions.

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

Using AND/OR mysql commands with FROM_UNIXTIME

Trying to select a query in php/mysql to get "Upcoming Items" in a calendar. We store the dates in the DB as a unix time. Here's what my query looks like right now
SELECT *
FROM `calendar`
WHERE (`eventDate` > '$yesterday')
OR (FROM_UNIXTIME(eventDate, '%m') > '$current_month' AND `$yearly` = '1')
ORDER BY `eventDate`
LIMIT 4
This is giving me an error "Unknown column '' in 'where clause'". I'm sure it has to do with my use of parenthesis (which I've never used before in a query) and the FROM_UNIXTIME command.
Can someone help me out and let me know how I've screwed this up?
Thanks!
This to me looks suspicious:
`$yearly`
What is the value of $yearly? Is it empty? When you use backticks MySQL treats the contents as the name of a column.
Perhaps you meant to create a string instead, in which case you should use a different type of quote:
'$yearly' = '1'
Or perhaps you intended to refer to the column yearly:
yearly = '1'
Another tip is to print the SQL query after the PHP variables have been interpolated as this sometimes makes it easier to understand the error message from MySQL. I'd imagine your query looks something like this:
SELECT *
FROM `calendar`
WHERE (`eventDate` > '1295071200')
OR (FROM_UNIXTIME(eventDate, '%m') > '1' AND `` = '1')
ORDER BY `eventDate`
LIMIT 4
The suspicious part is here:
`` = '1'
Do you have a column named $yearly ?, try removing the dollar sign

mysql NULL value in where in CLAUSE

how to deal with NULL value in mysql where in CLAUSE
i try like
SELECT * FROM mytable WHERE field IN(1,2,3,NULL)
it not working
only work like :
SELECT * FROM mytable WHERE field IN(1,2,3) OR field IS NULL
how can i get it work in WHERE IN ? it is possible ?
There is a MySQL function called COALESCE. It returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
If you for example run SELECT COALESCE(NULL, NULL, -1); you will get -1 back because it's the first non-NULL value.
So the trick here is to wrap your expression in COALESCE, and add a value as the last parameter that you also add in your IN function.
SELECT * FROM mytable WHERE COALESCE(field,-1) IN (1,2,3,-1)
It will only match if field is 1,2 or 3, or if field is NULL.
As by my understanding you want to pull every record with 1,2,3 and null value.
I don't think its possible to put null in the IN operator. Its expects values and null is well.. not a value. So You really have to put the OR with the null to get the desired result.
Maybe this information from the MySQL Reference Manual helps:
To comply with the SQL standard, IN returns NULL not only if the expression on the left hand side is NULL, but also if no match is found in the list and one of the expressions in the list is NULL.
Using UNION as a subquery in IN operator can get tableIds as a list and from that can get results with the NULL value.
eg:
SELECT * FROM
mytable
WHERE mytable.id IN(
SELECT mytable.id
FROM mytable
where mytable.field IS NULL
UNION
SELECT mytable.id
FROM mytable
WHERE mytable.field IN(1,2,3)
)
Following statement should help:
SELECT * FROM mytable WHERE COALESCE(field,0) IN (1,2,3,0)