How to retrieve distinct values from more than one column? - mysql

I have two columns of data. I'd like to run select statement on them that grabs all the distinct pieces of data in these columns 1 time. For instance.
*column 1* *column 2*
dog monkey
monkey elephant
dog monkey
I wanna be able to returna result set that has dog, monkey and elephant in it and that's all.

You can use two selects:
Select column1 As c From t
Union
Select column2 From t
Union will take care of duplicates:
The default behavior for UNION is that duplicate rows are removed from the result.

What about the following?
SELECT column1 unique_data FROM your_table
UNION
SELECT column2 unique_data FROM your_table;
Test case:
CREATE TABLE your_table (column1 varchar(50), column2 varchar(50));
INSERT INTO your_table VALUES ('dog', 'monkey');
INSERT INTO your_table VALUES ('monkey', 'elephant');
INSERT INTO your_table VALUES ('dog', 'monkey');
Result:
+-------------+
| unique_data |
+-------------+
| dog |
| monkey |
| elephant |
+-------------+
3 rows in set (0.00 sec)

It may be lil dumb: select distinct from (select distinct from first_column union select distinct from second) - not valid sql, just logic

Use UNION, sample:
select
column_1 as name
from Table_1
union
select
column_2 as name
from Table_1
Output:
dog
elephant
monkey

Related

Find Values between two different columns in a table

table one
+----------------------+
|column A | Column B|
| 2 | 4 |
| 3 | 5 |
| 1 | 2 |
| 1 | 2 |
| 8 | 7 |
+----------------------+
Output
+-------+
|1 | 2 |
|1 | 2 |
+-------+
i want to print only the above output without COUNT, and any duplicate record example? please help
how about below where cluase
select * from t where columnA=1 and columnB=2
or
select columnA,columnB from t
group by columnA,columnB
having count(*)>1
or you can use exists
select t1.* from t t1 where exists
(select 1 from t t2 where t2.columnA=t1.columnA
and t2.columnB=t1.columnB group by columnA,columnB
having count(*)>1
)
You possibly want only those rows which are duplicate. If you don't have Window Functions available in your MySQL version, you can do the following:
SELECT
t.*
FROM your_table AS t
JOIN (SELECT columnA, columnB
FROM your_table
GROUP BY columnA, columnB
HAVING COUNT(*) > 1) AS dt
ON dt.columnA = t.columnA AND dt.columnB = t.columnB
Details: In a Derived table, we get all those combination of columnA and columnB which have more than one row(s) (HAVING COUNT(*) > 1).
Now, we simply join this result-set back to the main table, to get those rows only.
Note: This approach would not be needed if you want to fetch only these two columns. A simple Group By with Having would suffice, as suggested in other answer(s). However, if you have more columns in the table, and you will need to fetch all of them, and not just the columns (used to determine duplicates); you will need to use this approach.
You can use in operator with a grouped subquery as :
select *
from tab
where ( columnA, columnB) in
(
select columnA, count(columnA)
from tab
group by columnA
);
or use a self-join as :
select t1.columnA, t1.columnB
from tab t1
join
(
select columnA, count(columnA) as columnB
from tab
group by columnA
) t2
on ( t1.columnA = t2.columnA and t1.columnB = t2.columnB );
Rextester Demo
I would use EXISTS, if the table has primary column :
SELECT t.*
FROM table t
WHERE EXISTS (SELECT 1 FROM table t1 WHERE t1.col1 = t.col1 AND t1.col2 = t.col2 AND t1.pk <> t.pk);

how to retrieve one row of many selected rows

I would like to select multi col across multi table in order to display them as only one row in MYSQL
like this example:
___________
|uId|xID|yID|
|_1_|_2_|_4_|
Into
___
|zId|
| 1 |
| 2 |
| 4 |
here is the query but col t returns null data beside all of the selected cols
SELECT `flaghsip_leader`,`clustuer_coordinator_id`,`clustuer_cocoordinator_id`,
`flagship_activity_coleader_id`,`flagship_activity_focalpoint_id`,
`output_leader_id`,`output_coleader_id` AS `t`
FROM crpcoreix.view_all_involved_users;
From what you've described, I think you'll need to use multiple select queries and join them with UNION ALL.
e.g.
SELECT uId as zId from Table1
UNION ALL
SELECT xId as zId from Table2
UNION ALL
SELECT zId from Table3

Sum multiple counts from multiple tables

I have different tables (per region) with some columns that are the same. Now I need to have a count for each value that is placed in one column over multiple tables. I'm trying to get a sum for this so I don't have to use 4 separate queries and outputs. Furthermore the values are matched with a lookup table
Region table(s) - Table1:
id | Column1 |
---------|----------|
1 | 1
2 | 2
3 | 3
etc
Lookup table
id | Description |
---------|-------------|
1 | Description1
2 | Description2
3 | Description3
The query I'm using to get the count from 1 of the tables is :
SELECT Description, Count(*) as Number from Table1, LookupTable
WHERE Column1 = LookupTable.id GROUP BY Column1 ORDER BY Number Desc
The output is
Description | Number
---------------|--------
Description1 | Number
Description2 | Number
Etc.
Any idea on how to sum up the counts for each Description/value of Column1 from 4 tables that generates the output as displayed above (but then with the sum value for each description)?
It's not clear but I guess you can use:
select LookupTable.id,LookupTable.Description, SUM(Cnt) as Number
from LookupTable
JOIN
(
SELECT Column1 as CId, count(*) as Cnt from Table1 group by Column1
union all
SELECT Column2 as CId, count(*) as Cnt from Table2 group by Column2
union all
SELECT Column3 as CId, count(*) as Cnt from Table3 group by Column3
union all
SELECT Column4 as CId, count(*) as Cnt from Table4 group by Column4
) T1 on LookupTable.id =T1.Cid
GROUP BY LookupTable.id,LookupTable.Description
ORDER BY Number Desc
Use this query:
SELECT LookupTable.Description, Count(*) as Number
FROM Table1, LookupTable
WHERE Table1.Column1 = LookupTable.id
GROUP BY Table1.Column1;
You have leave the name of table or its alias to call the column.

MySQL : How to know which table that record belongs to in UNION result

Lets say that I want to get the records from 2 table by using UNION.
How could I add a field to each record that would tell me which table it belongs to? It just something like this :
id | title | link | table
-----------------------------------------------------
1 | Title 1 | somelink.html | articles1
2 | Title 2 | link2 .html | articles2
3 | Title 3 | link3 .html | articles1
Thanks in advance?
select some_column, 'union_1' as from_where
from table1
union
select some_column, 'union_2' as from_where
from table2
You could try something like
SELECT Col1, Col2, 'Table1' TableSource
FROm Table1
UNION ALL
SELECT Col1, Col2, 'Table2' TableSource
FROm Table2
This will work/make sense for UNION ALL, but might be misleading if you use UNION, as duplicates will then be included due to the differentiating source column.
Just put it in your UNION, like:
SELECT *, 'articles1' AS table_name FROM articles1
UNION ALL
SELECT *, 'articles2' AS table_name FROM articles2

Query two columns into one?

Is there a way to select these two colums:
Name 1 | Name 2
John Paul
Paul Ringo
Ringo George
So as to get both in one column with no repeated values:
Names
John
Paul
Ringo
George
SELECT name1 FROM theTable
UNION DISTINCT
SELECT name2 FROM theTable
It sounds like you can use:
SELECT Name1 FROM Table1
UNION SELECT Name2 FROM Table1
That will perform duplicate row removal by default, or you can make it explicit like this:
SELECT Name1 FROM Table1
UNION DISTINCT SELECT Name2 FROM Table1
See the docs for UNION for more information.
MySQL supports the UNION command. You just need to make sure the columns returned are the same.
E.g.
SELECT customerNumber id, contactLastname name
FROM customers
UNION
SELECT employeeNumber id,firstname name
FROM employees