cross referencing records across tables - mysql

i have 10 similar tables in my database. all the tables have the same field email in them. i want to find out email addresses that occur in more than 3 tables. the field email is not a primary key.
Thanks in advance.

You could use a count function over a union all query:
SELECT email, COUNT(*) AS num_tables
FROM (SELECT DISTINCT email FROM table1
UNION ALL
SELECT DISTINCT email FROM table2
UNION ALL
-- All the other tables
) t
GROUP BY email
HAVING COUNT(*) > 1

Related

Select 3 columns to one without union

I try to select three columns (TEXT - The same type) to one column without using union (System required), but I can't think on right way to do so.
select count(distinct(r)) as count
from (
select column1 as r from Table1
union all
select column2 as r from Table1
union all
select column3 as r from Table1
) a
This is the original query,
I tried to use CONCAT and COALESCE, but it isn't what I want.
I don't want to merge two string into one,
I want that all the select line will be in the same column.
For example - example - img
3 columns - to, cc and bcc (email recipients).
I want to know how much people I send emails.
Without double the emails (everyone can be on any column)
Thank you for your help!
I appreciate it.
Either use COUNT(DISTINCT ...):
SELECT COUNT(DISTINCT email) FROM (
SELECT DISTINCT firstColumn AS email FROM table
UNION
SELECT DISTINCT secondColumn AS email FROM table
UNION
SELECT DISTINCT thirdColumn AS email FROM table) AS Recipients
Even better: since you're running DISTINCT on the outer query, don't waste time doing that in your inner queries:
SELECT COUNT(DISTINCT email) FROM (
SELEC firstColumn AS email FROM table
UNION
SELEC secondColumn AS email FROM table
UNION
SELEC thirdColumn AS email FROM table) AS Recipients
See example fiddle here: http://sqlfiddle.com/#!9/edda57/10
No UNION, but even uglier. I join the table with itself and take the first 3*TABLE_LENGTH rows, because I need to process three different columns.
SELECT count(DISTINCT if(rank<=#size, recipient, if(rank<=2*#size,cc,bcc)))
FROM
(SELECT (#rank:=#rank+1) AS rank,
s1.recipient,
s1.cc,
s1.bcc
FROM sent AS s1
JOIN sent AS s2) AS multi,
(SELECT #rank:=0) AS dummy1,
(SELECT #size:=
(SELECT count(1)
FROM sent)) AS dummy2
WHERE rank <= 3*#size
Fiddle: http://sqlfiddle.com/#!9/14ac8a/14/0
Simply use alias into a concat :) example here :
SELECT CONCAT(to, cc, bcc, year) as new_colum_mailing FROM `mailing` .. WHERE ...

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

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.

Select distinct emails from multiple tables

I have like 10 different tables all with different names, but they all contain an email_address column. I want to do a DISTINCT on all of the rows.
Pretty much my end goal is to get a number of distinct email addresses in a bunch of lists.
How could I do this?
I think you want something like this:
SELECT email_address FROM table1
UNION
SELECT email_address FROM table2
UNION
-- And so on...
Something like this
select email_address from table1
union
select email_address from table2
union
...
etc
UNION already removes duplicates. UNION ALL will include duplicates.
select email_address from c1
UNION
select email_address from c2
the UNION statement will remove all duplicate values.

MySQL NOT IN return a wrong number

I have to clean a database with a lot of orphaned entries, on this case i have a table 3 tables
'Email' (69529 entries)
'ServiceHasEmail' (5782 entries)
'UserHasEmail' (26254 entries)
The two last tables reference the 'Email' table, so this table should have 26254 + 5782 (32036) entries.
I built a query to select all the entries on the 'Email' table which are not referenced on the UserHasEmail and ServiceHasEmail tables:
SELECT * FROM Email e
WHERE e.EML_Id NOT IN (SELECT EML_Id FROM ServiceHasEmail)
AND e.EML_Id NOT IN (SELECT EML_Id FROM UserHasEmail)
But this query returns me 40383 entries in place of 37493 (69529 - (26254 + 5782))
What i am missing here?
This can be because
tables ServiceHasEmail and UserHasEmail contains some emails
both.
tables ServiceHasEmail and UserHasEmail contains duplicates.
You can verify:
select count(distinct email) from Email
select count(distinct email) from ServiceHasEmail
select count(distinct email) from UserHasEmail
and
select count(distinct Email)
from
(select Email from ServiceHasEmail
union all
select Email from UserHasEmail
)
And your query should be
SELECT count(distinct Email)
FROM Email e
WHERE e.EML_Id NOT IN (SELECT EML_Id FROM ServiceHasEmail)
AND e.EML_Id NOT IN (SELECT EML_Id FROM UserHasEmail)
Looks like some ServiceHasEmail and UserHasEmail reference the same Email.
You could have EML_Id's, that are present both in ServiceHasEmail and UserHasEmail.
I suppose you have exactely 2,890 of them. Please try
SELECT * FROM `ServiceHasEmail` INNER JOIN `UserHasEmail` USING(`EML_Id`)
to verify this.
you can use the following query:
SELECT * FROM Email e
WHERE e.EML_Id NOT IN (SELECT EML_Id FROM ServiceHasEmail UNION SELECT EML_Id FROM UserHasEmail)