SELECT current_raised,segment FROM gizmos
where created_at >= "2012-03-01"
returns 600+ rows
SELECT "current_raised","segment"
UNION
(SELECT current_raised,segment FROM gizmos
where created_at >= "2012-03-01")
returns 180 rows
Why is this happening?
union will remove from the result set every duplicated current_raised, segment pair. Try union all instead.
Here is a reduced example of what is happening.
It's a slightly confusing feature of standard SQL: the default qualifier for SELECT is SELECT ALL but the default qualifier for UNION is UNION DISTINCT.
Your first query is implicitly using SELECT ALL and is returning duplicate rows in the result, not good. Correct the first query by explicitly using SELECT DISTINCT.
Related
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 ;).
I have a query which may not return anything if there is no match (if uuid and scene_id do not match).
My query:
select active from likes where uuid=12 and scene_id=22
so inorder to solve the problem and return 0 in this situation; I am union it with a default value and add 'limit 1' like the following query; but I could not get it work!
select * from ((select active from likes
where uuid=12 and scene_id=22)as a
union all
(select 0 as 'active')as b)limit 1;
it gives me a syntax error on "as b "
What am I doing wrong in this case?
Please let me know if you need more clarification.
Change your query as below (remove the inner table alias)
select *
from
(
select active from likes where uuid=12 and scene_id=22
union all
select BIN(0) as 'active'
) tab limit 1;
Problem was that, active is of bit type and in second select 0 is INT type. Just convert 0 to binary representation using BIN() function.
I think you overdid the parentheses. This should work:
select
active
from
(select active, 0 as importance from likes
where uuid=12 and scene_id=22
union all
select FALSE, 1) x
order by importance
limit 1
Note the extra importance column. It makes sure that the actual value from Active is used. Otherwise, the actual row and the default may theoretically be returned in any order.
But I actually agree with Joe Stefanelli's comment, that your application should handle this rather than your query.
Example: http://sqlfiddle.com/#!2/7cfbe3/1
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
Is it possible in MySQL to select from a static set of integers? Given an array of integers like 1 and 2, what is the correct syntax in MySQL to do something like:
select
*
from
(values(1),(2))
Which should return one row containing the number 1 and one row containing the number 2.
In other SQL than MySQL (e.g. MSSQL) this is possible. Is there a way to do this in MySQL?
I think you mean something like this?
SELECT 1 UNION SELECT 2 UNION SELECT 3
The only way to create a virtual set in MySQL is using a subquery with UNION. The subquery in the FROM clause creates 3 rows which can be joined with another table:
SELECT foo.*
FROM (
SELECT 1 AS num UNION
SELECT 2 AS num UNION
SELECT 3 AS num
) virtual
LEFT JOIN foo ON foo.num = virtual.num
When you want to use your list of values as a condition for WHERE or ON clause, then the IN() construct may be the right way to go:
SELECT foo.* FROM foo WHERE foo.num IN (1,2,3)
sorry for my english
you can use (IN) like this
SELECT * FROM Table WHERE id IN (1,2,3....)
Am I correct in saying:
COUNT(expr)
WHERE expr IS NOT *
Will count only non nulls?
Will COUNT(*) always count all rows? And What if all columns are null?
Correct. COUNT(*) is all rows in the table, COUNT(Expression) is where the expression is non-null only.
If all columns are NULL (which indicates you don't have a primary key, so this shouldn't happen in a normalized database) COUNT(*) still returns all of the rows inserted. Just don't do that.
You can think of the * symbol as meaning "in the table" and not "in any column".
This is covered in the MySQL Reference Manual.
If you want to count NULLs as well, try
SELECT COUNT(IFNULL(col, 1)) FROM table;
just checked:
select count(*)
returns 1 with one record filled with NULLs
select count(field)
returns 0.
I don't see the point in the record with NULL values. Such record must not exist.
count(*) is not for non-null columns, it's just the way to ask to count all rows. Roughly equivalent to count(1).
Using MySQL I found this simple way:
SELECT count(ifnull(col,1)) FROM table WHERE col IS NULL;
This way will not work:
SELECT count(col) FROM table WHERE col IS NULL;
If you want to count only the nulls you can also use COUNT() with IF.
Example:
select count(*) as allRows, count(if(nullableField is null, 1, NULL)) as missing from myTable;
You can change the if condiditon to count what you actually want. So you can have multiple counts in one query.
select count(*) as 'total', sum(if(columna is null, 1, 0)) as 'nulos' from tabla;