Overall Unique count from two tables in MySQL - 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.

Related

return the same record more than once in a query result

The following query:
SELECT * FROM table WHERE id IN (1,2,3);
will return three records.
SELECT * FROM table WHERE id IN (1,2,1);
will return just two records (for Ids 1 and 2)
Is there a way for the result set to contain two records for Id 1 (and three in total)?
You could try creating a table for the ids you want to filter by. This would get you your desired results. I'm not sure if mysql supports CTE, but hopefully this is enough for you to get the idea.
WITH IDS
AS
(
SELECT 1 AS id
UNION ALL
SELECT 2 AS id
UNION ALL
SELECT 1 AS id
)
SELECT T.*
FROM T
JOIN IDS
ON T.id = IDS.id

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
);

Finding Unique Values from two or more tables

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

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.

Mysql select distinct

I am trying to select of the duplicate rows in mysql table it's working fine for me but the problem is that it is not letting me select all the fields in that query , just letting me select the field name i used as distinct , lemme write the query for better understading
mysql_query("SELECT DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id")
mysql_query("SELECT * , DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id")
1st one is working fine
now when i am trying to select all fields i am ending up with errors
i am trying to select the latest of the duplicates let say ticket_id 127 is 3 times on row id 7,8,9 so i want to select it once with the latest entry that would be 9 in this case and this applies on all the rest of the ticket_id's
Any idea
thanks
DISTINCT is not a function that applies only to some columns. It's a query modifier that applies to all columns in the select-list.
That is, DISTINCT reduces rows only if all columns are identical to the columns of another row.
DISTINCT must follow immediately after SELECT (along with other query modifiers, like SQL_CALC_FOUND_ROWS). Then following the query modifiers, you can list columns.
RIGHT: SELECT DISTINCT foo, ticket_id FROM table...
Output a row for each distinct pairing of values across ticket_id and foo.
WRONG: SELECT foo, DISTINCT ticket_id FROM table...
If there are three distinct values of ticket_id, would this return only three rows? What if there are six distinct values of foo? Which three values of the six possible values of foo should be output?
It's ambiguous as written.
Are you looking for "SELECT * FROM temp_tickets GROUP BY ticket_id ORDER BY ticket_id ?
UPDATE
SELECT t.*
FROM
(SELECT ticket_id, MAX(id) as id FROM temp_tickets GROUP BY ticket_id) a
INNER JOIN temp_tickets t ON (t.id = a.id)
You can use group by instead of distinct. Because when you use distinct, you'll get struggle to select all values from table. Unlike when you use group by, you can get distinct values and also all fields in table.
You can use DISTINCT like that
mysql_query("SELECT DISTINCT(ticket_id), column1, column2, column3
FROM temp_tickets
ORDER BY ticket_id");
use a subselect:
http://forums.asp.net/t/1470093.aspx