How can I get the result by only one sql in MySQL - mysql

There two mysql table just like table1 and table2. I want get the result by one sql.
#table1
c1 c2 //clomun
a 10
b 20
c 30
#table2
c1 c2
a 11
b 21
e 99
I want get the result like below.
# result
c1 c2
a 21
b 41
c 30
e 99

This should work:
select c1, sum(c2) from
(
select c1, c2 from table1
union all
select c1, c2 from table2
) as total
group by c1
Please note that if the columns names are not identical, you will need to give them identical alias names as below:
select column1, sum(column2 ) from
(
select c1 as column1, c2 as column2 from table1
union all
select c1 as column1, c2 as column2 from table2
) as total
group by column1

Related

CTE passing multiple subqueries

This is an extension to my previous post.
WITH cte1 AS (
SELECT * FROM Combination
WHERE Col1 = 'val' and city='karim'),
cte2 AS (
SELECT * FROM Combination
WHERE Col1 = 'val2' and city='karim')
SELECT CONCAT(cte1.Col2, cte2.Col2) AS Result
FROM cte1 CROSS JOIN cte2;
col1
col2
City
Val
145
Telang
val2
13
Telang
val2
25
Telang
val
146
karim
val2
124
karim
val2
56
karim
Output:
Result
14513
14525
146124
14656
There are multiple cities.I wanted to get combinations only for the values existing in the cities
Tried something like this, but does not work.
SELECT * FROM Combination
WHERE Column1 = 'value' and city IN(select city from Combinations);
Use an INNER self join of the table:
SELECT CONCAT(c1.Col2, c2.Col2) AS Result
FROM Combination c1 INNER JOIN Combination c2
ON c2.city = c1.city
WHERE c1.Col1 = 'val' AND c2.Col1 = 'val2';

How to make a template table in SQL that pulls specific columns from other different tables

I have multiple tables with multiple columns, but for this question say I just have three tables with one column each:
table1:
id
A
B
1
20
14
2
11
table2:
id
C
D
E
100
14
4
101
16
12
19
table3:
id
F
1234
6
8765
11
Desired output:
mainTable:
id
tableName
columnName
value
1
table1
A
20
1
table1
B
14
2
table1
B
11
100
table2
C
14
101
table2
C
16
101
table2
D
12
101
table2
E
19
8765
table3
F
11
As you could notice, I'd like the query to also have a condition where it only chooses to insert into the main table if the column value is greater than 10.
Let me know if I can add any further information to the question.
you can use union and a CTE for clarity to insert your rows with a single insert
with t as (
select id, 'table1' Tablename, 'col1' ColumnName, col1 as Value
from table1
union all
select id, 'table2', 'col2', col2
from table2
union all
select id, 'table3', 'col3', col3
from table3
)
insert into mainTable (id, tableName, columnName, Value)
select id, tableName, columnName, Value
from t
where value > 10
You want to create a so-called key/value table. They are generally a nuisance to work with, but you will have your reasons. The query to get from a normal table to a key/value table is to select value by value and union the results.
select id, 'table1' as table_name, 'A' as column_name, a as value from table1 where a > 10
union all
select id, 'table1' as table_name, 'B' as column_name, b as value from table1 where b > 10
union all
select id, 'table2' as table_name, 'C' as column_name, c as value from table2 where c > 10
union all
select id, 'table2' as table_name, 'D' as column_name, d as value from table2 where d > 10
union all
...

Delete duplicate elements in SQL

How to delete duplicate elements in SQL?
That is mean in each column, each element should only occur once.
For example, I have a table like:
NAME1 NAME2 NAME3 NAME4
A1 A2 A3 A4
A1 B2 A3 A4
A1 C2 C3 B4
B1 C2 B3 C4
C1 B2 A3 B4
There are so many duplicate elements in each column and they are placed randomly.
I should convert it to the table like below:
NAME1 NAME2 NAME3 NAME4
A1 A2 A3 A4
B1 B2 B3 B4
C1 C2 C3 C4
Well, I finally found out a solution to my problem.
Select the distinct names in each column as tables and then inner join them with adding common rownumbers could work.
However, this problem could be solved since the number of distinct names in each column are equal. I am still trying to find out how to solve the problem when the number of distinct names in each column are not equal.
set #r1 = 0, #r2=0, #r3=0, #r4=0;
select A.n1, B.n2, C.n3, D.n4 from
(select *,
case when n1 is not null then (#r1:=#r1+1) end as Rownumber
from(
select distinct NAME1 n1
from MYTABLE)Tn1)A
inner join
(select *,
case when n2 is not null then (#r2:=#r2+1) end as Rownumber
from(
select distinct NAME2 n2
from MYTABLE)Tn2)B
on A.Rownumber = B.Rownumber
inner join
(select *,
case when n3 is not null then (#r3:=#r3+1) end as Rownumber
from(
select distinct NAME3 n3
from MYTABLE)Tn3)C
on A.Rownumber = C.Rownumber
inner join
(select *,
case when n4 is not null then (#r4:=#r4+1) end as Rownumber
from(
select distinct NAME4 n4
from MYTABLE)Tn4)D
on A.Rownumber = D.Rownumber;

SQL select rows having count > certain number

I don't really know how to search for this, probably it's quite easy to do it, but I don't know how to do this.
I have a SQL table:
| c1 | c2 | c3 | c4 | c5 |
data data data data data
So I've 5 columns, and now I want to select the rows with only the following (c1, c2, c3) where that row appears more than 5 times in the table
Something like this:
Select c1, c2, c3
From table
having count(*) > 5 and (all in that count, all rows must have the same values on c1, c2, c3)
Can only do this with basical sql queries. Functions, declarations and etc are not allowed.
Don't really know if i'm explaining myself well.
Not absolutely sure I understand, but my guess would be
select c1, c2, c3
from <yourtable>
group by c1, c2, c3
having count(*) > 5
This query will return all records from your original table whose c1, c2, and c3 combined values appear in duplicate more than 5 times. I also included the actual count in the result set.
SELECT t1.c1, t1.c2, t1.c3, t1.c4, t1.c5, t2.cardinality
FROM yourTable t1
INNER JOIN
(
SELECT c1, c2, c3, COUNT(*) AS cardinality
FROM yourTable
GROUP BY c1, c2, c3
HAVING COUNT(*) > 5
) t2
ON t1.c1 = t2.c1 AND
t1.c2 = t2.c2 AND
t1.c3 = t2.c3
Just treat c1,c2,c3 as a single string c1+c2+c3:
SELECT c1, c2, c3 FROM table WHERE
c1 || c2 || c3 IN (
SELECT c1 || c2 || c3 FROM table
GROUP BY c1 || c2 || c3
HAVING COUNT(*) > 5);

Mysql using count in query

Say that I have two tables T and T1
T
id p o
1 47 1
2 48 2
3 49 25
T1
id p o
1 47 1
2 42 2
3 47 25
I am looking to insert rows from T1 into T if count(T1.p)>1
T
id p o
1 47 1
2 48 2
3 49 25
1 47 1
3 47 25
I tried the following query but it didn't work
insert into T(id , p,o)(SELECT T1.id , T1.p1,T1.l FROM T1
where SELECT count(*) FROM t1
GROUP BY t1.p
HAVING COUNT(*)>1)
For more details .
Any help will be appreciated .
To get those values into T you will have to find out who they are in T1 and JOIN them with T1 again, to get the right number of rows:
INSERT INTO T (id, p, o)
SELECT TT.*
FROM T1 TT
INNER JOIN (
SELECT id, p1, l
FROM T1
GROUP BY p1
HAVING COUNT(*) > 1
) a ON a.p1 = TT.p1;
sqlfiddle demo
How this works:
SELECT id, p1, l
FROM T1
GROUP BY p1
HAVING COUNT(*) > 1
Returns the p1 that appears more than once in the table. This returns p1 = 47. GROUP BY p1 HAVING COUNT(*) > 1 makes sure that for each p1, we only want the results that appear more than once.
Then, we do an inner JOIN with T1 again, to get all rows that have P1 = 47:
ID P1 L
1 47 1
3 47 25
Then you just INSERT this result in the destination table.
You have a couple of errors in your select.
This should get you going:
SELECT T1.id , T1.p1,T1.l
FROM t1
GROUP BY t1.p1
HAVING COUNT(*)>1
SQL Fiddle
EDIT: Updated the SQL Fiddle to include the insert.
insert into T SELECT T1.id , T1.p1,T1.l FROM T1
GROUP BY t1.p1
HAVING COUNT(t1.p1)>1
http://www.sqlfiddle.com/#!2/75c8e/1
Use dml on the left side
The below SQL should do what your looking for:
INSERT INTO T (id, p, o)
SELECT id, p1, l
FROM T1
WHERE p1 IN (
SELECT p1
FROM T1
GROUP BY p1
HAVING COUNT(*) > 1
);
SQL Fiddle - Final result