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.
Related
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;
I have two tables Staffs and Customers I want to select the records of each independent of each other but in one query in a such a way that neither of them affects each others' records. for example
Selecting the customers,
Selecting staffs records
,
What I want
As long JOINING and UNION is not for this purpose
You can use Join
Select * from Staffs inner join Customers;
If you just want to get them together into one dataset, you don't need a concatenation. You just need a UNION.
I don't know the exact name of your tables or which RDBMS you are using, but you might try something like:
SELECT *
FROM Staffs
UNION ALL
SELECT *
FROM CUSTOMERS;
If you have the same column names in each, you could do:
SELECT ColumnA, ColumnB, ColumnC AS "Staffs_ColumnC"
FROM Staffs
UNION ALL
SELECT ColumnA, ColumnB AS "Customers_ColumnB", ColumnC
FROM Customers;
It sounds like you want to union them together:
select id, name, lastname, 'staff' as which
from staffs
union all
select id, name, lastname, 'customer'
from customers;
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 ...
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
I'm making this select:
select code,name from talbe1
union all
select code,name from table2
The actual code isnt important to me but what is important to me is that the code column will be unique column, and with this select I cant grantee it..
Is there any save word/something that will give me something like that:
select getUniqueCode(),name from(
select name from talbe1
union all
select name from table2)
Thanks.
Have a look at the mysql UUID call. Which would result in something like this:
select UUID(),name from(
select name from talbe1
union all
select name from table2)
remove "all":
select code,name from table1
union
select code,name from table2
Union all keeps al rows.
Union removes duplicates.
If you have different names for the same code in each table, you have to pick one name - try this:
select code, max(name) as name
from (select code,name from table1
union
select code,name from table2) x
group by 1