MySQL UNION SELECT and IN clause - mysql

I have two very simple table: t1 and t2 with the following rows:
table t1:
id, name
1 PBN
table t2:
id, name
100 FIBERHOME
Query 1:
SELECT name FROM t1 UNION SELECT name FROM t2 WHERE id IN (1)
Result is: PBN
Query 2:
SELECT name FROM t1 UNION SELECT name FROM t2 WHERE id IN (100)
Result is: PBN, FIBERHOME
But the expected result is: FIBERHOME..! What is the reason?

To expand on #Knep's answer, if you only want one WHERE id IN ():
SELECT name FROM (
SELECT id, name FROM t1
UNION
SELECT id, name FROM t2
) unioned
WHERE id IN (1,100)
Probably not great speed wise, so best to test.
Note also the id needs to be in the sub query to be used in the outer WHERE.
I thought that the WHERE clause is global – #szpal
To answer the question as to why the WHERE isn't used for all queries in the UNION, think about two queries that don't share a column.
On their own:
SELECT id, name FROM x WHERE colA = 123
And:
SELECT id, name FROM y WHERE colB = 456
Then together with (the incorrect) single WHERE clause:
SELECT id, name FROM x
UNION
SELECT id, name FROM y
WHERE colB = 456 -- But table x doesn't have a colB!
Whereas if (correctly) the WHERE clause sits with each query:
SELECT id, name FROM x
WHERE colA = 123 -- I have a colA, still don't have a colB
UNION
SELECT id, name FROM y
WHERE colB = 456 -- I have a colB, still don't have a colA
Everyone's a winner!

UNION sum up the two results.
In the first query, there is no condition so it returns PBN, then it adds the result of the second result FIBERHOME.
Using UNION you could try:
SELECT name FROM t1 WHERE id IN (100) UNION SELECT name FROM t2 WHERE id IN (100)

The where condition in second query will be executed before union.
SELECT name FROM t1
will return
id name
1 PBN
SELECT name FROM t2 WHERE id IN (100)
will return
id name
null null
The union will combine above two results as
SELECT name FROM t1 UNION SELECT name FROM t2 WHERE id IN (100)
id name
1 PBN
You can solve this by
SELECT
name
FROM
(SELECT
*
FROM
interns_test_db.t1 UNION SELECT
*
FROM
interns_test_db.t2) A
WHERE
ID IN (100)
But this may reduce the performance.

Related

MySql: How to select rows where all values are the same?

I have a table like this:
name |id | state
name1 12 4
name1 12 4
name2 33 3
name2 33 4
...
I want to select every name and id from table where state is only 4, that means name1 is correct, because it only has two records with state 4 and nothing more. Meanwhile name2 is wrong, because it has record with state 4 and record with state 3.
You can use aggregation as shown below:
SELECT name, id
FROM your_table
GROUP BY name, id
HAVING SUM(state<>4)=0;
See a Demo on SQL Fiddle.
select name, id from mytable where id not in
(select distinct id from mytable where state <> 4)
you might need 2 sub queries .
select with group by name were state 4
select with group by name
compare the count if the count is same then select it
example : select name , count (name) from table where state = 4 as T1
select name , count (name) from table as T2
select T1.name from T1 and T2 where T2.count = T1.count
You can use not exists like this:
select distinct name, id
from table1 a
where not exists (select *
from table1 b
where a.id=b.id and state<>4)
In a more general case you can use count distinct (with not exists or with a join):
select distinct name, id
from table1 a
where not exists (
select *
from table1 b
where a.id=b.id
group by id
HAVING count(distinct state)>1)

query to combine the result of two columns into one with alternate values in MYSQL

I need a query to combine the two different columns of the same table in a single column with alternate values.
Table: tblDemo
Id | Name
_________
1 | A
2 | B
3 | C
Required output:
Result
______
1
A
2
B
3
C
Assuming name is not null, you can use
select coalesce(name, id) as Result from
(
select id, name from tblDemo
union all
select id, null from tblDemo
) as subquery
order by id, name;
you can use UNION
select id as result from tbldemo union select name as result from tbldemo
If Id is varchar/nvarchar then:
select Result from (
select Id as 'Result' from tblDemo
union all
select Name as 'Result'from tblDemo) A
And if Id is int then you should use this:
select Result from (
select CAST(Id as nvarchar(max)) as 'Result' from tblDemo
union all
select Name as 'Result'from tblDemo) A
SELECT id value,id my_order FROM my_table
UNION
SELECT name,id FROM my_table
ORDER
BY my_order,value;

MySQL select all subquery rows with minimum value

I'd like to select all rows of a subquery with the minimum value in a given field. Here's some toy examples of the techniques I've tried so far:
-- 1.
select
id, min(foo)
from
(select 1 AS id, 2 AS foo
union select 2 AS id, 2 AS foo
union select 3 AS id, 3 AS foo) a;
-- 2.
select
min(foo)
from
(
select 1 AS id, 2 AS foo, 0 AS const
union select 2 AS id, 2 AS foo, 0 AS const
union select 3 AS id, 3 AS foo, 0 AS const) a
group by const;
-- 3.
select
id
from
(select 1 AS id, 2 AS foo
union select 2 AS id, 2 AS foo
union select 3 AS id, 3 AS foo) a
where id = (select id from a where min(foo) = foo);
-- 4.
select
id
from
(select 1 AS id, 2 AS foo
union select 2 AS id, 2 AS foo
union select 3 AS id, 3 AS foo) a
where foo = (select min(foo));
-- 5.
select r.*
from
(
select min(foo) t
from
(select 1 AS id, 2 AS foo
union select 2 AS id, 2 AS foo
union select 3 AS id, 3 AS foo) a
) m
INNER JOIN a ON m.t = r.foo;
The actual query I'm working on is similar to the examples, in that it's made up of several smaller queries UNIONed together. The overall goal here is to lookup a row in a central table based on the fields of an association table k to which it is joined, where k is highest priority table. The result is a sort of tree view of rows from similar (but different tables).
I've mentioned this so in case someone can see that I'm going about this in a roundabout way they can shed some light on the bigger picture. But for now my angle is to select by a taking a minimum value on a field in the subquery.
Use order by and limit:
select t.*
from t
order by foo
limit 1;
Note: this only returns one row with the minimum, even if there are duplicates. The t is your subquery or table.
If you want all of them, then you need to include the table definition twice:
select t.*
from t
where t.foo = (select min(t2.foo) from t t2);

Sql query to Add non digit values of multiple columns into a single column and find the count

I have one table with two columns like this
Column1| Column2
A | A
B | C
D | D
The out put of the query as i expect is :
Name | Count
A | 2
B | 1
C | 1
D | 2
It is easy to sum two(values) but how do i concatenate nonvalues inside a table.
The query i tried:
select Column1 as Name from table union all select Column2 as Name from Table
I get the combined version of two columns but how am i suuposed to get the count(Name) ?
This will work perfectly..
SELECT t.Name, COUNT(t.Name) AS Count
FROM (
select `Column1` as Name from table
union all
select `Column2` as Name from table
) t
GROUP BY t.Name
Try this
Select Name, Count = count(*)
from (Select Name = column1 from table union all select column2 from table)t
group by Name
I think these SQl will help you.
select * from (
select column1 as col,count(Column1) as count from table_1 GROUP BY col
union all
select column2 as col, count(Column2) as count from table_1 GROUP BY col ) as table_alias group by col
Thank you.

How to Select max Value from 2 tables in MySQL

I have 2 tables in MySQL DB.
Both the tables have a column as ID which is of type int(10) unsigned.
Table1 has no data and Table2 has the ID as 24.
I am using the below query to get the max ID
select max(ID) from
(
select IFNULL(max(ID),0) as ID from table1
UNION
select IFNULL(max(ID),0) as ID from table2
)temp;
I am expecting the value 24 but it gives 0.
Anything wrong in my query? Please help.
try this,
SELECT IFNULL(MAX(ID), 0) ID
FROM
(
SELECT ID FROM table1
UNION ALL
SELECT ID FROM table2
) a