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

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)

Related

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 UNION SELECT and IN clause

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.

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.

Mysql: Compare fields of 3 columns

I have a table that looks like this:
id name dob
007 name1 19680514
007 name2 20110830
16842 name3 19660927
250718 name3 19660927
253692 name4 19350328
25576 name5 19520813
25576 name5 19520813
I need:
- a SELECT statement that gives me every row where the id is the same as in another row, but the correspoinding name or dob differ.
example output:
id name dob
007 name1 19680514
007 name2 20110830
a second statement that gives me every row where the name and (!) dob are the same as in another row, but the id differs.
example output:
id name dob
16842 name3 19660927
250718 name3 19660927
Background:
Completely identical rows are allowed, but:
- every id should be unique for a combination of name&dob
- every combination of name&dob should have only one id assigned.
So I want to find out the entries with errors to be able to manually correct them.
Thank you!
First query:
SELECT * FROM your_table yt
INNER JOIN (
SELECT
id
FROM
your_table
GROUP BY id
HAVING COUNT(*) > 1
) sq ON yt.id = sq.id
Second query:
SELECT * FROM your_table yt
INNER JOIN (
SELECT
name, dob
FROM
your_table
GROUP BY name, dob
HAVING COUNT(*) > 1
) sq ON yt.name = sq.name AND yt.dob = sq.dob
SELECT statement that gives every row where the id is the same as in another row, but the corresponding name or dob differ. example output:
select * from tbl t1
join tbl2 t2
on t1.id = t2.id
where (t1.name <> t2.name) or (t1.dob <> t2.dob)
Statement that gives every row where the name and (!) dob are the same as in another row, but the id differs. example output:
select * from tbl t1
join tbl2 t2
on t1.name = t2.name AND t1.dob = t2.dob
where (t1.id <> t2.id)

MySQL - count values and merge results from multiple tables

I have two tables with one column each, containing names.
Names can have duplicates. One name can be found on every table or only in one.
I want to make an query that count duplicates, for each name in every table an list these values like this:
| name | table1 | table2 |
| john | 12 | 23 |
| mark | 2 | 5 |
| mary | | 10 |
| luke | 4 | |
I tried different strategies using UNION but no luck.
Thanks in advance!!!
SELECT DISTINCT t1.name, t1.cnt1, t2.cnt2
FROM
(SELECT name,count(name) as cnt1 FROM table1 GROUP BY name) t1
LEFT JOIN
(SELECT name,count(name) as cnt2 FROM table2 GROUP BY name) t2
ON t1.name = t2.name
UNION
SELECT DISTINCT t2.name, t1.cnt1, t2.cnt2
FROM
(SELECT name,count(name) as cnt1 FROM table1 GROUP BY name) t1
RIGHT JOIN
(SELECT name,count(name) as cnt2 FROM table2 GROUP BY name) t2
ON t1.name = t2.name
Here's a simpler solution:
You can UNION the names from the two tables together, manually differentiating their origin tables with a tbl column.
Then it's just a simple GROUP BY with conditional aggregation using the differentiating column:
SELECT a.name,
NULLIF(COUNT(CASE a.tbl WHEN 1 THEN 1 END), 0) AS table1,
NULLIF(COUNT(CASE a.tbl WHEN 2 THEN 1 END), 0) AS table2
FROM
(
SELECT name, 1 AS tbl FROM table1 UNION ALL
SELECT name, 2 FROM table2
) a
GROUP BY a.name
In accordance with your desired result-set, we NULL the count value if it turns out to be 0.
SQLFiddle Demo
SELECT SUM(res.cn), name
FROM
(
SELECT name, count(name) as cn from table1 GROUP BY name HAVING count(name) > 1
UNION ALL
SELECT name, count(name) as cn from table2 GROUP BY name HAVING count(name)>1
) as res
GROUP BY nam
e
Try the above :) I made a fiddle for you to test it:
http://sqlfiddle.com/#!3/796b2/3
It has a few double names in each table and will show you which names have doubles and then print them. The names that only appear once are not shown (acheived by the HAVING clause)
After some reading i don't think that it's posibil what i want to do. This situation ca be solved with pivot table in excel or libreoffice.
In fact this is method that i used, combined with some sql stataments to count occurence of names and export as CSV.
UNION definitetly not work. Some chance are with join, but not shure.
I found a post that discusses the same problem as mine.
MySQL - Rows to Columns