Select 3 columns to one without union - mysql

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 ...

Related

How to duplicate returning values from a query in MySQL

When I use this I Get the table two times but I want each value duplicated before the next value comes
SELECT *
FROM student
WHERE major LIKE 'C%'
UNION ALL
SELECT *
FROM student
WHERE major LIKE 'C%';
You can use a union all. A union will not work, because it will eliminate duplicates. Another way is a cross join:
select
*
from
students t1 cross join
(select 1 as n union all select 2) n
WHERE major LIKE 'C%'
order by id;

Mysql Union in different columns

I have the following query
SELECT *
FROM(
(SELECT
MAX(c.start_time) as start_1
FROM
c1 c)
UNION ALL
(SELECT
MAX(cc.created_at) as ccmax
FROM
cc1)
) as t
I'd like to have the result in a table with 2 columns start_1 and cmax instead of the single column I get with all the different results listed.
How should I do it? I ended up in a subselect believing this would have done the job.
For the data to be in two columns you would have to use a sub select.
SELECT
MAX(c1.start_time) as start_1, (SELECT MAX(cc1.created_at) FROM cc1) as ccmax
FROM c1

cross referencing records across tables

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

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)