i am using mysql ifnull in query, if the id is null then it should return 1 but it is not working, i think the syntax is good... what is the problem?
the query i am using is:
SELECT IFNULL(id,1) autoid FROM tb_orders
You may try with COALESCE like this:
SELECT COALESCE(id,1) autoid FROM tb_orders
Just to add after watching your comments, the above query is usefull only when your table has records on it. Querying on emplty table is like trying to get icecream from the empty refrigerator! ;-)
For handling empty table you need to check if number of rows returned is zero or not
SELECT case COUNT(*) when 0 then 1 else IFNULL(id,1) END as autoid
FROM tb_orders
SQL Fiddle Demo for Empty table
But in my opinion you should use other value than 1 for autoid when table is empty to differentiate between non empty table with NULL value and empty table.
SELECT case COUNT(*) when 0 then -1 else IFNULL(id,1) END as autoid
FROM tb_orders
Related
I have created date and resolved date in Table1. Both fields are included in one record.
ID created_date resolved_at
'1xxxx' '18-04-2018' '20-04-2018'
Is it possible that i may use custom SQL function to separate them into 2 records.
I want somehow output like this.
ID date Operation
1xxx. 18-04-2018. Created
1xxx. 20-04-2018. Resolved
SELECT ID,created_date DATE,CASE WHEN CREATED_DATE IS NOT NULL THEN 'Created' end OPERATION FROM TABLE1
UNION
SELECT ID,resolved_at DATE,CASE WHEN resolved_at IS NOT NULL THEN 'Resolved' end OPERATION FROM TABLE1
You simply need to use CASE WHEN and according to which ever column is not null we will make that name as OPERATION.
You can check SQL fiddle here
Try above query.
It will help you.
The simpler solution is:
select id, created_date, 'created' as operation from t
union all
select id, resolved_at, 'resolved' as operation from t;
I have a query like this:
SELECT id
FROM table
GROUP BY columnName, CASE WHEN columnName IS NULL THEN id ELSE 0 END
The purpose of the CASE statement in the GROUP statement is to prevent rows with null in columnName (the column value I'm using to group rows together) from being grouped together. I only want to group together rows that have non-null values in columnName.
The problem is that the CASE statement increases the execution time of the query from 0.09s to 0.854s, an increase of about 10x.
Using EXPLAIN, I can see that without the CASE statement, an index for columnName is used, but with the CASE statement no index is used, which I'm assuming is why the query becomes so much slower?
Is there any way to prevent the CASE statement from slowing the query down so much? For example, is it possible to index the CASE statement?
I'm very inexperienced with SQL, so the only solution I can think of is to create another column, named something like columnName_or_id, and to include logic in my software that makes another query after inserting the row to put the id into columnName_or_id, and if later a value is inserted into columnName to also insert that value into columnName_or_id (hence the name of the new column, as it either contains the row id or the columnName value). That would eliminate the need for the CASE statement in the query.
Is there a better way?
Try COALESCE()
GROUP BY coalesce(columnName, id)
Perhaps it would be faster to do a union all in this case:
(SELECT id
FROM table
WHERE columnName is not null
GROUP BY columnName
)
union all
(select id
from table
where columnName is null
)
If where columnName is not null prevents the use of the index, then try having columnName is not null.
I was surprised to see one of my students writing
select count(title='Staff') from titles
against MySQL employees database. Does it mean to be a nice shortcut for
select sum(case when title='Staff' then 1 else 0 end) from titles
which just fails to work in MySQL 5.6 (returning full table count)?
No, this count is not a shorthand for the sum you wrote.
COUNT(exp) counts the number of rows with a not null exp.
title='Staff' is a Boolean expression, which evaluates to true if title is 'Staff', false if it's any other value or null if it's null.
So, this query is equivalent to
select count(title) from titles
which, in turn, is equivalent to
select count(*) from titles where title is not null
No!
You can't count against an condition write into brackets. It simple return all rows from that table.
select count(title='Staff') from titles
return all rows from titles
I have one table ABC with EMPLID, GRADE, SALARY and DATE as its fields.
I am executing the following 3 statements:
select count(*) from ABC;
Result :- 458
select count(*) from ABC where GRADE LIKE '%def%';
Result :- 0
select count(*) from ABC where GRADE NOT LIKE '%def%';
Result :- 428
My point here is: the result of second query plus the result of third query should be equal to the result of first query, shouldn't it?
Looks like you have 30 records where the GRADE is null.
null values are unknown, so do not match either condition.
Sql uses a three-valued logic: true, false &unknown. If you compare a NULL to any other value the result is unknown. NOT(unknown) is still unknown.
A WHERE clause only returns rows that evaluate to true. So the missing 30 rows in your example have a NULL in the Grade column.
Note that apart from the obvious case where you can have NULL values in your table (as others have mentioned), you could also have empty strings.
In Oracle:
-- Returns nothing
select 1 from dual where '' like '%'
In MySQL:
-- Returns 1
select 1 from dual where '' like '%'
Since you tagged your question with both oracle and mysql, you might be running into an incompatibility here, as in oracle '' IS NULL holds true!
I need to export data from an existing TABLE. Till recently I used -
SELECT item_ID, item_Info, ...moreFields... FROM tableName WHERE myCondition=0
Now, they changed the TABLE structure, and added new field "item_Info2"
When they fill in the TABLE:
if "item_Type" is 1 or 2 or 3 or 4, then "item_Info" is relevant
if "item_Type" is 5, then "item_Info" is empty, and I need "item_Info2" as my query result, instead of "item_Info"
What is the corresponding SELECT command?
[similiar question: mysql select any one field out of two with respect to the value of a third field
but from this example I cannot see the syntax for selecting moreFields ]
Thanks,
Atara.
You can treat the CASE statement as any other column name, separate it from the other columns with a comma, etc. What happens in the CASE should be considered a single column name, in your case you need commas before and after it.
SELECT item_ID, CASE WHEN item_Type = 5 THEN item_info ELSE item_info2 END, field_name, another_field_name ...moreFields... FROM tableName WHERE myCondition=0
you could also use an alias to be easier to get the result from the query:
SELECT item_ID, CASE WHEN item_Type = 5 THEN item_info ELSE item_info2 END AS 'item_info', field_name, another_field_name ...moreFields... FROM tableName WHERE myCondition=0
To edit the reply on the other question:
SELECT
item_id, item_otherproperty,
CASE WHEN item_Type= 5
THEN item_Info2
ELSE item_Info
END AS `info` FROM table ...