Select all items with a field nonempty - mysql

I am trying to select all the rows from a table with a field non-empty. My sql query is
SELECT * FROM coupon_category WHERE customer=4 AND like_at <>;
But its shows syntax error in the end. What is the correct query? like_at is a datetime type

Assuming like_at is a string, a simple way is:
WHERE customer = 4 AND like_at <> ''
Of course, it depends on what you mean by non-empty.
This would normally refer to NULL, so for any type would be:
WHERE customer = 4 AND like_at IS NOT NULL
Note: The first version also filters out NULL values as well as empty strings (assuming the type of like_at is a string).

If you want to exclude blanks, and potentially nulls, try:
SELECT * FROM coupon_category WHERE customer=4 AND ifnull(like_at,'') <> '';

Query interpreter expects a value after <> signs.
If you want to select all rows with a field not empty, you should do:
SELECT * FROM coupon_category WHERE customer=4 AND like_at <> '';
Maybe you may check null values also.

That depends what happend when like_at is empty, wthen the "cell" is empty it gives you null?
If the cell is empty u can use
SELECT * FROM coupon_category WHERE customer=4 AND len(like_at)>0
And if it gave you null then :
SELECT * FROM coupon_category WHERE customer=4 AND like_at is not null

Related

Why does <> 'null' works in MySQL?

Hope the question is not too generic. Couldn't find anything on the site or in SQL documentation:
While coding, i tested this, and to my surprise it worked:
SELECT * FROM cal_entry WHERE cal_entry.parent_id <> 'null'
It actually shows the rows without the ones with NULL values (these are real NULL values in database, not strings with 'null' inside).
According to the docs, I should have used NOT NULL, of course. By the way, it doesn't work with = 'null', like it is correctly stated in the docs.
Can someone explain that?
You are selecting all rows where <> 'null' is true.
Comparing(equals or not-equals) to null is null, so if a row where cal_entry.parent_id is null, your condition will be false/null.
So your query gets all rows that are not null, nor contain the string 'null'.
(Note, you could just as well have written <>'something_else')
Assuming parent_id in an int column the query will return all non-null, non-zero rows:
SELECT *
FROM (
SELECT NULL AS parent_id UNION ALL
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 2
) AS cal_entry
WHERE cal_entry.parent_id <> 'null'
-- returns 1 and 2 but not 0!
When comparing a number to string MySQL will convert the string to number. Some examples:
'null' becomes 0
'asdf' becomes 0
'1asdf' becomes 123
'1' becomes 1
Your query will behave like:
WHERE cal_entry.parent_id <> 0
this operator give you result of not equal to. ex. $var != null.
we write in mysql as <>. this is kind of validation that the value shoud never be equal to null.
When working with null following two statements should always be taken note of -
An expression can be null, but it can never be equal to null.
Two nulls are never equal to each other.
So, in your query wherever there is a comparison null<>null it returns true by second statement.
Also, always account the possibility that some rows might contain null -
Select * from cal_entry where cal_entry.parent_id!=10
This query would leave out the rows with null entries. Instead use -
Select * from cal_entry where cal_entry.parent_id!=10 or cal_entry.parent_id is null

MySQL: can anyone explain about using = to select not null rows

I'm a beginner MySQL user.
My teacher gave me a question to explain how this function works.
SELECT *
FROM TableName
WHERE ColumnName=ColumnName
Then, the result shows the rows that contain values in that column. (The null value is not appear)
I have no idea about it. I do searching for answer but most of it is talking about using IS NOT NULL.
You need to use ' = ' to select rows by checking an empty column right ?
If that's the case
You can simply use the below code
SELECT *
FROM TableName
WHERE ColumnName = ' '
The expression:
WHERE ColumnName = ColumnName
is comparing two values from the same column. This should be true in all cases, except when ColumnName contains a NULL value. So, you can equivalently write this as:
WHERE ColumnName IS NOT NULL
This version is more understandable and the preferred way to write the logic.

MySQL comparison with null value

I have a column called CODE in a MySQL table which can be NULL. Say I have some rows with CODE='C' which I want to ignore in my select result set. I can have either CODE=NULL or CODE!='C' in my result set.
The following query does not return a row with CODE as NULL:
SELECT * from TABLE where CODE!='C'
But this query works as expected and I know it is the right way to do it.
SELECT * from TABLE where CODE IS NULL OR CODE!='C'
My question is why does having only CODE!='C' does not return rows where CODE=NULL? Definitely 'C' is not NULL. We are comparing no value to a character here. Can someone throw some light as why it doesn't work that way?
In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Take a look at this MySQL Reference on NULL.
Any arithmetic comparison with NULL does not return true or false, but returns NULL instead., So, NULL != 'C' returns NULL, as opposed to returning true.
Any arithmetic comparison with 'NULL' will return false. To check this in SQL:
SELECT IF(NULL=123,'true','false')
To check NULL values we need to use IS NULL & IS NOT NULL operator.
Based on my tests and the documentation here: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
You can compare null and get a boolean result using <=>
NOTE: it looks like NOT EQ operator, but it's EQ operator
For example:
select x <=> y;
or
select #x <=> #y;
This also compares string vs null, string vs string, etc.
In SQL, the NULL value is a special value, not comparable with any other one.
The result of a direct comparison with a NULL is always NULL, although (unfortunately) you may find FALSE in some implementation.
To test a null value you should use IS NULL and IS NOT NULL.
SELECT *
FROM `table_name`
WHERE IFNULL(`column_name` != 'C', TRUE)
The specified problem can also appear in joins and the above answers aren't particularly helpful. The way I prefer to do it is by coalescing to otherwise impossible value. For example, this
select foo from bar
inner join baz on bar.x = baz.y
won't work if bar.x and baz.y are both nulls (join won't bring results). The workaround is to use e.g.
select foo from bar
inner join baz on coalesce(bar.x, -1) = coalesce(baz.y, -1)
where -1 is "impossible" value meaning it can never appear in the data set.
select * from user where application_id='1223333344' and name is null;
I use:
SELECT * from TABLE where NOT(CODE <=> 'C')

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)

SELECT WHERE field!=value how it's done in mysql?

I can't find the answer since searching mysql NOT in google is a nightmare (even with the quotes).
I need to make a query like this:
SELECT * FROM table WHERE field=value AND field2!=value2 AND field3!=value3
How it is done? Is it even possible?
SELECT * FROM table WHERE
((field = value) AND
(field2 <> value2) AND
(field3 <> value3))
If you're dealing with NULL, you have to do two things:
Use SET ANSI_NULLS ON
Declare NULL values to a dummy value.
SQL Cannot compare nulls.
To do that:
SET #value = ISNULL(#value, -1);
Yes, you can do exactly what you wrote, but use <> instead of !=
Perhaps the answer depends on what "value" is? For example, for an integer 123 value would be 123; for a string "foobar" value would be 'foobar'.
have you tried the <> operator
SELECT * FROM table WHERE field = value AND field2 <> value2
have you tried "<>"? it works in Delphi