Finding Unique Values from two or more tables - mysql

I have two tables, training_taken_november and qtr1_copy. Each of them have a unique field. I want to join these two tables and count the unique values from both tables where their category = 'livestock', illustration below:
Tbl1 values might be: 1,2,3,4,5
Tbl2 values might be: 6,7,8,1,2
The count of distinct values should return: 8, that is, when we join the two tables they have 8 unique values in mentioned fields (name of fields that have unique values also differ).

Select count(*) from
(
Select id from ... Where category...
Union
Select id from ... Where cat ...
) x

Try this:
Select count(*) from
(select tazkira from Training_taken_november where category='Livestock'
union
select bene from qtr1_copy where cat='Livestock')x

Related

Excluding column from MySQL SELECT distinct query with UNION across tables

I have an SQL database with several tables of patient data. Every table has one column in common, an ID number representing each patient. There is significant overlap between the tables, i.e. the same patient ID number often appears on multiple tables. What I would like to do is SELECT all distinct patient ID numbers that do not appear on one specific table.
You can use UNION and NOT IN like this:
select id
from (
select id from table1
union
select id from table2
union
select id from table3
...
) t where id not in (
select id from sometable
);

In MYSQL, is it possible to get records from two tables where both the tables have comma separated values?

I am having problem to find records from two tables where both the tables have common name field and both fields have comma separated values.
For instance,
table-1 have "a,b,c" value
id | name
----------
1 | a,b,c
and table-2 have "a,c,d,e,f" value
id | name
---------------
1 | a,c,d,e,f
Now I want to compare both tables where at least one character matches in both.
So is it possible to get records where at least one character matches into both fields or not?
Thanks in advance :)
First, use the UNION statement to combine rows in both tables; include only the columns that need to compare. The returned result set is used for the comparison. Considering table-1 as t1 and table-2 as t2.
SELECT t1.pk, t1.c1
FROM t1
UNION ALL
SELECT t2.pk, t2.c1
FROM t2
Second, group the records based on the primary key and columns that need to compare. If the values in the columns that need to compare are identical, the COUNT() returns 2, otherwise the COUNT() returns 1.
See the following query:
SELECT pk, c1
FROM
(
SELECT t1.pk, t1.c1
FROM t1
UNION ALL
SELECT t2.pk, t2.c1
FROM t2
) t
GROUP BY pk, c1
HAVING COUNT(*) = 1
ORDER BY pk
MySQL compare two tables example
SELECT id,title
FROM (
SELECT id, title FROM t1
UNION ALL
SELECT id,title FROM t2
) tbl
GROUP BY id, title
HAVING count(*) = 1
ORDER BY id;
It will return unmatched records.

Retrieve records on matching a field in any 2 tables out of 4 tables

I have four tables with same fields. Now I want to join these tables in such a way that I retrieve records only if there is a match between any two tables on a field(like name).
Thanks in advance.
This would return all the name values that appear in more than one table:
select
name
from
(select distinct
name
from table1
union all
select distinct
name
from table2
union all
select distinct
name
from table3
union all
select distinct
name
from table4) temp
group by name
having count(*) > 1;
Check out the interactive example.

Overall Unique count from two tables in MySQL

I have two tables, both having column a device_id column that I want to count. For the purposes of demonstration, the schema looks like:
Table 1: 'id', 'save_val', 'device_id_major'
Table 2: 'id', 'save_val', 'location', 'device_id_team'
Table 1 could have many of the same 'device_id_major'.
I basically want to get the unique device_id's from both tables, then from that result set, get the count of unique device_id's (the same device_id can appear in both tables).
Is this possible in one query?
select distinct aa.device_id, count(*)
from(select distinct device_id from table1
union all
select distinct device_id from table2) as aa
group by device_id
order by device_id
Or something like... As I don't have the schema to hand, I can't fully validate it.
SELECT count(DISTINCT aa.id)
FROM (SELECT DISTINCT major_id AS id FROM `major`
UNION ALL
SELECT DISTINCT team_id AS id FROM `team`)
AS aa
This seems to do the trick.
You could use a query that takes the UNION of both tables, then SELECT the unique values.

MySQL: Select Distinct from 2 different tables?

I have 2 different tables that each have a column called product_type. How can I get the DISTINCT values of product_type across both tables? Just to clarify, if both tables have a product_type of "diamond" I only want it returned once. Basically as if both tables where combined and I selected distinct product_type from it.
Thanks!!
Use a distinct with a subquery which contains a union
select distinct product_type from (
select product_type from table 1
union
select product_type from table 2
) t
Use distinct and union:
select distinct product_type from table1
union
select distinct product_type from table2
The union will remove duplicates when combining the results.
Note that the UNION clause returns unique values of the field, when you want it to return ALL the values you must use UNION ALL...
select product_type from table_a
union
product_type from table_b