Mysql Query to Merge 3 tables into 1 Table - mysql

TO Plot Each Input table I have Separate query, need to apply functionality on that queries and want to create Single query for Output Table
Select Distinct Names, SUM(count) from
(Select Query table 1
union
Select Query table 2
union
Select Query table 3) table group by Names;
this query Not adding count properly Niether Sorting Names properly Whats wrong with this ?
Input Table 1 :-
Names count
bob 3
pol 4
Input Table 2 :-
Names count
bob 5
0 - name may be missing here neglect this entry
Input Table 3 :-
Names count
james 4
pol 7
bob 1
Expected output table :-
Names count
bob 9
pol 11
james 4

You can use UNION and them sum of those.
select sum(a), sum(b) from
(select 2 as a, 1 as b
union select 3 as a, 6 as b
union select 4 as a, 1 as b) as b

Try this query
select `Name`,sum(`Count`) total from ( select `Name`,`Count` from `table1` union all select `Name`,`Count` from `table2` union all select `Name`,`Count` from `table3` ) tot group by `Name`
May this help you.

Related

MySQL Add interger of multiple columns together as result [duplicate]

I have a table with 3 columns (A,B,C). I want to select some rows from the table and then the MySQL to return a single row having the values added on each column.
A B C
1. 2 2 2
2. 4 4 4
3. 6 7 8
MySQL should return in this case, if I select all the three rows:
A B C
1. 12 13 14
select sum(A),sum(B),sum(C) from mytable where id in (1,2,3);
select
sum(a) as atotal,
sum(b) as btotal,
sum(c) as ctotal
from
yourtable t
where
t.id in (1, 2, 3)
Try this:
select sum(a), sum(b), sum(c)
from your_table

How to find proper User between two tables

I have something like this in two tables on my database
"User" table
userId viewsIDS
1 1,2,3,4,5
2 2,4,6,12,13
3 1,4,5
... ...
"View" table
viewID webpageId
1 1
2 1
3 2
4 3
5 3
6 3
... ...
Now, which query should I use to find user IDs just having the webpage IDs?
I guess should somehow group "viewIDs" from "View" table and then check if one of these IDs is in the User "viewIDS" field so take the userID out, but I don't know which query would make this the better way possible.
You can try this:
CREATE TABLE #USER (userId INT, viewsId VARCHAR(50))
CREATE TABLE #VIEW (viewId INT, webPageId INT)
INSERT INTO #USER
SELECT 1, '1,2,3,4,5'
UNION SELECT 2, '2,4,6,12,13'
UNION SELECT 3, '1,4,5'
INSERT INTO #VIEW
SELECT 1,1
UNION SELECT 2,1
UNION SELECT 3,2
UNION SELECT 4,3
UNION SELECT 5,3
UNION SELECT 6,3
SELECT u.userId, v.webPageId FROM #USER u
CROSS APPLY fnSplitString(viewsId, ',') s
INNER JOIN #VIEW v ON s.splitdata = v.viewId
ORDER BY u.userId
I copy this function fnSplitString in:
http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/
But as was told before, you should normalize your table ;)

Explode prevsiouly rolled up data in SQL

I have data as follows:
Product Quantity
A 3
B 2
This is data that as been previously rolled up at the product level. Assume there are only two columns as of now.
I want an output as follows:
Product Quantity
A 1
A 1
A 1
B 1
B 1
You could use a trick like this:
SELECT Product, 1 AS Quantity
FROM
Products INNER JOIN (
SELECT 1 AS q UNION ALL
SELECT 2 UNION ALL SELECT 2 UNION ALL
SELECT 3 UNION ALL SELECT 3 UNION ALL SELECT 3
) quantities
ON Products.Quantity = quantities.q
of course, this query is limited to a quantity of 3, but you can add more quantities to your subquery if they are of a limited amount.

SQL query of multiple values in one cell

There is a table(Course Interests) which has all the values in one cell. But those values are just ids and I want to join them with another table(Course) so I can know their names.
Course Interests:
MemberID MemberName CoursesInterested
-------------- --------------------- --------------
1 Al 1,4,5,6
2 A2 3,5,6
Course Table:
CourseId Course
-------------- ---------------------
1 MBA
2 Languages
3 English
4 French
5 Fashion
6 IT
Desired Output:
MemberID MemberName CoursesInterested
-------------- --------------------- --------------
1 Al MBA,French,Fashion,IT
2 A2 English,Fashion,IT
I would like to do a SQL query in MySql that can help me to extract the desired output. I know how to do it in the opposite way(join values to one cell), but I've struggling on seek a way to separate the ids and do a cross-join into another table.
I'll appreciate any help from the community. Thanks
Use FIND_IN_SET to search for something in a comma-delimited list.
SELECT i.MemberID, i.MemberName, GROUP_CONCAT(c.Course) AS CoursesInterested
FROM CourseInterests AS i
JOIN Course AS c ON FIND_IN_SET(c.CourseId, i.CoursesInterested)
However, it would be better to create a relation table instead of storing the courses in a single column. This type of join cannot be optimized using an index, so it will be expensive for a large table.
Try this Out:
SELECT MemberID,MemberName,Group_Concat(C.Course) from
(
SELECT MemberID,MemberName,SUBSTRING_INDEX(SUBSTRING_INDEX(t.CoursesInterested, ',', n.n), ',', -1) value
FROM Table1 t CROSS JOIN
(
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n
) n
WHERE n.n <= 1 + (LENGTH(t.CoursesInterested) - LENGTH(REPLACE(t.CoursesInterested, ',', '')))
ORDER BY MemberID,value
) T JOIN course C ON T.value = C.CourseId
Group By MemberID,MemberName
Fiddle Demo
Output:
MemberID MemberName CoursesInterested
-------------- --------------------- --------------
1 Al MBA,French,Fashion,IT
2 A2 English,Fashion,IT

mysql join two tables

table a
id title
1 aaa
2 ccc
table b
id title categories
1 123 24
2 222 5
3 333 6
How to join table a and table b like this:
id title categories
1 aaa
2 ccc
1 123 24
2 222 5
3 333 6
Thanks a lot.
select id, title, null as categories from a
union all
select id, title, categories from b
You can use UNION ALL as:
SELECT id,title,'' AS categories FROM a
UNION ALL
SELECT id,title,categories FROM b
Since UNION ALL expects the two result sets to have same number of fields I've added field named categories in the first query whose values is always empty string.
You can use UNION to combine the data from the two tables such as:
Select id, title, "" as categories from tableA
UNION ALL
Select id, title, categories from tableB
In the first select query, empty string is provided as categories for column matching. UNION requires identical columns in both select queries. Also UNION ALL lists out the duplicates as well.