SELECT WHERE field!=value how it's done in mysql? - 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

Related

MySQL - IF Query with STR_TO_DATE

Greetings Stackian Overflowers,
I'm trying to overwrite data in an existing column and do a STR_TO_DATE in the case it is already set.
Here's the code :
SELECT IF(columnA = '../../..' , '0000/00/00' , STR_TO_DATE(columnA, '%m/%d/%Y)
FROM tablename
WHERE extract_date = '2018-12-31';
In all reality, I must use this logic to make an update on the table. I tried this :
UPDATE tablename
IF columnA = '../../..' THEN
SET columnA = '0000-00-00' ELSE
SET columnA = STR_TO_DATE(columnA,'%m/%d/%Y);
Could use any help on the matter.
MySQL's REGEXP operator comes in handy here:
UPDATE tablename
SET date_col = CASE WHEN columnA REGEXP '[0-9]{2}/[0-9]{2}/[0-9]{4}'
THEN STR_TO_DATE(columnA, '%m/%d/%Y)
ELSE NULL END;
It doesn't make sense to update columnA from a string to a date, because the types don't match. A better strategy is to create a new column to store the date. Also, I recommend using NULL, not 0000-00-00, to represent a date string that could not be parsed.
Note that the regex I used is not foolproof and does not completely validate your date strings. But it should at least weed out string data which is really off.

MySQL update query with 'concat()' value based on a 'CASE' not working

I am trying to update the a column value by appending a string to itself based on if the column value doesn't already have it. I thought I had it right but am unable to get it to work. Can someone help me understand if I am heading in a wrong direction. I would also appreciate if you could show me if there is a better way of doing this.
use my_database;
$additional_condition = 'string with special chars and quotes';
update my_table set my_column = concat(my_column, CASE WHEN my_column LIKE '%$additional_condition%' THEN $additional_condition ELSE '' END) where my_name in ('x', 'y');
If I understand correctly, you should move the condition to the WHERE clause:
update my_table
set my_column = concat(my_column, $additional_condition)
where my_name in ('x', 'y') and
my_column not like '%$additional_condition%';

Select all items with a field nonempty

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

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')