How to put IFNULL - mysql

I have column in table1 where name column has lot of rows as null
table1.name=table2.userID
So I m trying to put IFNULL condition as this
select (IFNULL(name,'empty'))
But My output table after exceution comes blank.

It should work
SELECT IFNULL(`name`, 'empty') FROM `tablename`

Related

SQL select values from an array which are not in my table

I have an array ('abc','def','ghi','jkl'). I want to find the values which are not in mysql table.
That is, if 'def'is not in the table, then it should show 'def' and so on.
My query was:
SELECT column FROM table WHERE column not in ('abc','def','ghi','jkl')
But its wrong. How can I get the values which are not there in the column?
You should put these values first in some table and then do "Not in" like :
SELECT column FROM table WHERE column not in (select distinct col1 from table1).
Here is how you can do it:
select x.col
from (values row('def'),row('abc'),row('ghi')) x(col)
left join table t on t.col = x.col
Where t.col is null

MYSQL - calculated column aliases?

I want to have a SELECT statement name columns based on other column values.
Let's say I have a table with column names like q_1, q_2 and other columns like q_1_name and q_2_name
Right now we are doing something like
SELECT SUM(q_1), SUM(q_2) from mytable;
I'd like to get a result set with the columns named for the values in q_1_name and q_2_name
SELECT SUM(q_1) as (q_1_name), SUM(q_2) as (q_2_name) from mytable;
Any chance you know a way to do this?
You can use a simply alias AS
SELECT SUM(q_1) as q_1_name, SUM(q_2) as q_2_name from mytable;
or using a subselect
select t.q_1_name, t.q_2_name
from (
SELECT SUM(q_1) as q_1_name, SUM(q_2) as q_2_name from mytable
) t;

How to get all columns from a SELECT DISTINCT query?

I'm using PDO with MySQL.
I want to select all rows from a given table with distinct values in a given column, but SELECT DISTINCT column_name FROM table returns the rows with only that column_name. Therefore I can't access the other row's columns.
I've been searching for answers and it looks like SELECT DISTINCT column_name FROM table is supposed to return all the rows with distinct values inside column_name with all the row's columns. However, I only get the column I want distinc'ed:
Array
(
[image] => leather_helmet.jpg
// there are supposed to be more fields here...
)
May this be a PDO's bug or am I doing something wrong?
Thanks in advance! :)
If you want only 1 column distinct you have to think of which record you want for the other columns. For instance if you like the min id record for the distinct column then you can do
SELECT *
FROM armor_unsealed
WHERE id IN
(
SELECT min(id)
FROM armor_unsealed
WHERE piece=:piece
GROUP BY image
)'

How to return NULL when result is empty?

I have a simple query that selects one field and only one row, thus one value.
Is there any way to make it return NULL if the query results in an empty set? Instead of returning zero rows?
I think I need to use something with NOT EXISTS, THEN NULL but not certain about it.
select
(Your entire current Select statement goes here) as Alias
from
dual
dual is a built in table with a single row that can be used for purposes like this. In Oracle this is mandatory. MySQL supports it, but you can also just select a single value without specifying a table, like so:
select
(Your entire current Select statement goes here) as Alias
In either case you're selecting a single value. This means that:
If your select returns one value, that value is returned.
If your select statement returns one column, but no rows, NULL will be returned.
If your select statement returns multiple columns and/or multiple rows, this won't work and the query fails.
An easy way to do this is with aggregation:
select max(col)
from t
where <your condition here>
This always returns one row. If there is no match, it returns NULL.
Late reply but I think this is the easiest method:
SELECT
IFNULL((SELECT your query), NULL)
Use a UNION with a NOT EXISTS(original where clause)
select col1
from mytable
where <some condition>
union
select null
where not exists (
select * from mytable
where <some condition>)
You can use COALESCE for example:
SELECT COALESCE(Field1,NULL) AS Field1 FROM Table1
Edit 1:
sorry i mistake with return field as null not result set,for result set return as null use Union and Exist Function like this:
SELECT NULL AS Field1 FROM Table1 WHERE not EXISTS(SELECT Field1 FROM Table1 WHERE Field2>0)
UNION
SELECT Field1 FROM Table1 WHERE Field2>0

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)