select all columns, simultaneously doing a ifnull on one - mysql

I want to be able to do a select on all columns, displaying a 0 (for a few of them) if null, without having to write each of the columns' names in the statement.
All I could think of is something like this:
SELECT *, IFNULL(`nullable_col1`, 0) FROM `my_table`;
What's the right way to do this?

No there is no way. You have to use the IFNULL function on each column which you want to have the value for.
One thing which you can do is that, you can simply select the value for all the columns which are not NULL(but I am not sure if that is what you want)
SELECT * FROM my_table WHERE (nullable_col1 AND nullable_col2 AND nullable_col2) IS NOT NULL
So this will select only columns which are not NULL.

Related

SQL Select Max of Columns Where Date is Not Null

I currently am using this query to select some data:
SELECT DISTINCT a.code AS code, name, max(scen.Is3D) AS Is3D FROM locations LEFT JOIN .... The scen table has columns Is3D and Date. I only want to select the max of items where the date IS NOT NULL. I tried max(scen.Is3D WHERE scen.Date IS NOT NULL), but that didn't work. I cannot change anything after the FROM in my query, so I need that filtering to be done in the MAX, if possible. I am using MySQL 5.7.
You can use:
MAX(CASE WHEN scen.date IS NOT NULL THEN scen.Is3D END) AS Is3D
The CASE expression returns NULL when none of the WHEN conditions is met, but MAX() ignores null values, so this will just return the max of the Is3D columns in the selected rows.
So if we can't change anything after the FROM, then we cannot get a perfect solution here. Since you are SELECTing out the NULL values. One thing that we can try if we can only modify the final output is this.
SELECT MAX(ISNULL(scen.Date,0))...
This will replace all the NULLs with 0, but it would help to know exactly what you are trying to do. Why are you so convinced that the query itself cannot be modified in any way?
The other solution would be to put the whole query in another wrapper.
That would look like:
SELECT *
FROM (
[your whole query here]
) AS inner
WHERE inner.Date IS NOT NULL

mysql SELECT WHERE LIKE multiple conditions

Nothing is being returned when I execute this query!
I'm trying to have multiple values for same column
SELECT * FROM `users` where Number='1212' AND Number='0921'
please, can you help?
Change the 'AND' to an 'OR'. A number (or anything else in a table column for that matter) cannot be both values at the same time
SELECT * FROM `users` where Number='1212' OR Number='0921'
Where you need rows where an element can be one of a number of values, it is common to use IN instead of repeated OR clauses, as this makes the expression clearer:
SELECT * FROM `users` where Number IN ('1212','0921');
As one of your Number values has a leading zero, I presume it is genuinely a string. You might want to name your columns better.
Or better use the IN operator, so you could filter for even more values:
SELECT * FROM `users` where Number IN ('1212', '0921');
Try this:
SELECT * FROM users where Number=1212 OR Number=0921
Change AND by OR, if you need to check equality with more values you could just use SELECT * FROM users WHERE number IN(1212, 0921, 1452, 1265);

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 can I use column relative position in if statement in order by?

I run a SQL query like below in MySQL:
select *
from (
select 2 as o,1 as t from dual
union
select 1 as o,2 as t from dual
) x
order by if((select 1),o,t);
It works well, but when I use column relative position in if statement, it doesn't work.
How can I use column relative position in if in ORDER BY statement?
select *
from (
select 2 as o,1 as t from dual
union
select 1 as o,2 as t from dual
) x
order by if((select 0),1,2);
I'm not sure what your real confusion is. When an integer appears in an order by, then this is treated as a column number. Any other use of an integer is interpreted as an expression.
The use of column numbers has been removed from the SQL standard. Hence, its use in any particular database is not guaranteed in future releases. It is really better to use the column names.
I think you want to sort your query based on a criteria over two columns, if I'm correct, you can use something like this:
...
order by
case when (your criteria)
then column1
else column2
end;
Note: use union all instead union when you don't want to remove duplicate values as performance issue ;).

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