mysql join two tables - mysql

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.

Related

Mysql count(*) based in two relations

I would like to count(*) how much customers have created a post or made a comment. If the same customer has made several posts and comments, it should count only once.
Customer Table:
ID Name ...
1 Jonh
2 Mark
3 King
4 Doe
Post Table:
ID USER_ID...
1 1
2 1
3 3
4 1
Comment Table:
ID USER_ID...
1 1
2 3
3 3
4 4
It should return count(*) = 3
(user_id: 1, 3 and 4).
Try this one. It worked for me and returns what you're looking for:
SELECT COUNT( USER_ID ) AS TOTAL
FROM (
SELECT USER_ID
FROM POSTS
UNION
SELECT USER_ID
FROM COMMENTS
)X
I used POSTS and COMMENTS as table names bc I was unsure what your exact table names are, so make sure to change these in your query.
This should work:
SELECT COUNT(DISTINCT USER_ID) FROM (
SELECT USER_ID FROM POST_TABLE
UNION
SELECT USER_ID FROM COMMENT_TABLE
)

Mysql Query to Merge 3 tables into 1 Table

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.

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.

Is it possible to join two tables into one single table , both tables have the same column names ...?

Is it possible to join two tables in mysql into one single table , both tables have the same column names ...?
table a has
id name state
1 jose up
2 sam mp
3 jack tn
table b is
id name state
4 ken ker
5 sk wb
is it possible to join both like:
id name state
1 jose up
2 sam mp
3 jack tn
4 ken ker
5 sk wb
Use UNION ALL
SELECT
id,name,state
from
tbla
UNION ALL
SELECT
id,name,state
from
tblb
If you want the exact duplicates to be excluded from the output. Use UNION
SELECT
id,name,state
from
tbla
UNION
SELECT
id,name,state
from
tblb
Reference:
13.2.8.4 UNION Syntax
The requirement you are describing is a union all query, not a join:
SELECT id, name, state
FROM table_a
UNION ALL
SELECT id, name, state
FROM table_b

SQL, add column with summed values of the same type

My current table looks like this:
ID TYPE QUANTITY
1 A1 3
2 B1 2
3 A1 2
4 B1 8
And after doing the query I want to get that:
ID TYPE QUANTITY SUM
1 A1 3 5
2 B1 2 10
3 A1 2 5
4 B1 8 10
The SUM column consist of summed quantities of items with the same type.
My approach is to use a derived table which aggregates the quantity by type first and then join this result with the original data:
select
t.id,
t.type,
t.quantity,
tmp.overall
from
table t join (
select
table.type,
sum(table.quantity) as overall
from
table
group by
table.type
) tmp on t.type = tmp.type
SELECT t.ID,t.TYPE,t.QUANTITY,x.SUM FROM TABLE t LEFT JOIN
(SELECT ID,TYPE,QUANTITY,SUM(QUANTITY) AS SUM FROM TABLE GROUP BY TYPE)x
ON t.type=x.type
SQL Fiddle
I haven't tried the query but see what happens if you do this:
SELECT
ID,
myTable.TYPE,
QUANTITY,
aaa.summy
FROM myTable
JOIN
(
SELECT
TYPE,
SUM(QUANTITY) summy
FROM myTable
GROUP BY TYPE
) aaa
ON aaa.TYPE = myTable.TYPE