Join table two column as 1 column - mysql

May I know how to join two columns table into as 1 column?
For example:
Edited
first_table
id | folder | category id | status
1 Peter 5 0
2 John 6 1
3 Shawn 7 0
second_table
id | filename| category id
1 123.sql 9
2 you.png 12
3 it.pdf 11
I want expected result like below, column folder and filename become store in 1 column and
column name as folder_filename :
First table join Second table
id | folder_filename| category id | status
1 Peter 5 0
2 John 6 1
3 Shawn 7 0
4 123.sql 9
5 you.png 12
6 it.pdf 11
I tried below this sql, but not sure how to modify make it work.
SELECT * FROM first_table INNER JOIN second_table
Hope someone can guide me how to solve it. Thanks.

You seem to want union all:
select f.id, f.folder, f.category_id
from first_table f
union all
select s.id, s.folder, s.category_id
from second_table s;
EDIT:
If you actually want to change the ids, then:
select row_number() over (order by which, id) as id, folder, category_id
from ((select f.id, f.folder, f.category_id, 1 as which
from first_table f
) union all
(select s.id, s.folder, s.category_id, 2
from second_table s
)
) fs
) fs

Related

Selecting rows with reference id's to same table

I have a table call user such as below:
id name ref_id
-- ---- ------
1 Sam 0
2 Jack 1
3 Sue 2
4 Sam2 1
5 Sue2 3
6 Sam3 1
7 Alan 3
8 Tom 3
9 Lyn 2
10 Van 1
I want to select the user have referred 3 people.
Is it possible to write a query and select this:
Name
--
Sam
Sue
You can use self join like below
SELECT a.`name` FROM ref a LEFT JOIN ref b
ON a.id = b.ref_id
GROUP BY a.name
HAVING COUNT(b.id) >2
Try this Demo
you can use query below:
select Count(u1.name) unit,u1.name from ref as u1 INNER JOIN ref as u2 ON u1.Id = u2.ref_id
group By u1.id
having unit > 3
you can replace unit > 3 to unit > 2

How to sum counted rows if same source id

Good Day,
I was creating a function where a user can share a post and other users may "LIKE or Comment" on it..
now on my POST table I have "id" and "source_id" + other details, to track people who like a post I created a table dedicated for like details. with columns: ID, USER_ID ( id of use who liked the post ), post_id
posts table:
id | source_id | caption
1 1 original post
2 1 share from id: 1 posts
3 3 new posts
4 4 new posts
likes table:
id | post_id | user_id
1 1 2
2 1 10
3 2 11
4 2 4
5 2 20
6 3 11
7 4 19
8 4 10
in order to count the number of like for a post, I do
SELECT a.id, a.source_id,b.num_likes
FROM posts AS a
LEFT JOIN (SELECT COUNT(user_id) AS
num_likes,post_id
FROM post_likes GROUP BY post_id ) AS b
ON b.post_id= a.id
WHERE b.num_likes != 0
result:
id | source_id | num_likes
1 1 2 <--- original post
2 1 3 <--- shared post who also got likes
3 3 1
4 4 2
what I would like to achieve is like these
id | source_id | num_likes
1 1 5
3 3 1
4 4 2
is this possible to achieve by just a query ? if so, could you please help me achieve this..
Thank you very much!
You can join the tables directly.
SELECT MIN(a.id), a.source_id, COUNT(b.num_likes) FROM posts AS a
LEFT JOIN post_likes AS b ON b.post_id= a.id
GROUP BY a.source_id
HAVING b.num_likes > 0
Might need to use a MIN for a.id since it is not aggregated. It would depend on your data and use case.
Use SUM function to get the total likes.
SELECT min(a.id) as id, a.source_id,sum(b.num_likes) as num_likes
FROM posts AS a
LEFT JOIN (SELECT count(1) AS
num_likes,post_id
FROM post_likes GROUP BY post_id ) AS b
ON b.post_id= a.id
WHERE b.num_likes != 0
GROUP BY a.source_id

How to write a query for selecting from three tables and if the values are null should not select?

I have three tables
1) Category_master
CID CTYPE
---------------------
1 2
--------------------
2 2
--------------------
3 1
-------------------
4 3
----------------
5 2
-------------------
6 3
2) NSS_maste
NID MID Name Value
1 5 Red 86
2 1 Blue 96
3) Sports_master
SID MID Name Cat
1 4 Walk Leg
The above is my table
I have to select CTYPE, NSS.NAME NSS.value,sports.name, sports.cat from category master,NSS_master and sports_master an it should not select null value Eg: for CID 2 there is no value in NSS_master table so it should not select but for CID 3 CTYPE 1 select name and other values as null
How to implement this thank you
Use Inner Join to avoid null values:
SELECT C.CTYPE, N.NAME, N.value, S.name, S.cat
FROM Category_Master C Inner Join NSS_master N ON C.CID = N.MID
Inner Join Sports_Master S ON C.CID = S.MID

Attempting to join and group data

I have two tables. The first is property and the second is category
The structure of the property table is:
id propertyname
1 a
2 b
3 c
4 d
and the structure of category is:
id propid catid
1 1 2
2 1 3
3 2 1
4 3 1
5 3 2
6 4 3
I'm trying to create a result like this:
id propertyname propid catid
1 a 1 2,3
2 b 2 1
3 c 3 1,2
4 d 4 3
How can I get this result? I used a group by clause and group_concate() function but it doesn't seem to work.
Here is what I've tried:
SELECT prop.*,category.property_id cat_prop,category.type_id type
FROM tbl_property prop,
tbl_category_type category
WHERE prop.id=category.property_id
Try this:
SELECT t0.id, t0.propertyname, group_concat(t1.catid)
FROM t0
INNER JOIN t1 ON t0.id = T1.propid
GROUP BY id
That should give you your result

how to get sum of two tables linked to a third

would any sql Wizards out there help with with this question:
suppose I have 3 tables:
tbltype tblvalue tblcost
id | type id | val | typeid id | cost| typeid
---------- ------------------ ------------------
1 | aaa 1 | 3 | 1 1 | 5 | 1
2 | bbb 2 | 2 | 1 2 | 3 | 1
3 | 2 | 2 3 | 1 | 2
4 | 1 | 2 4 | 4 | 2
When I run this query:
SELECT t.type, SUM(val), SUM(cost)
FROM
tbltype t
LEFT JOIN tblcost c ON (c.typeid = t.id)
LEFT JOIN tblvalue v ON (v.typeid = t.id)
GROUP BY t.type;
I get the wrong value of
type | SUM(val) | SUM(cost)
---------------------------
aaa | 10 | 16
bbb | 6 | 10
how do I get the right value of:
type | SUM(val) | SUM(cost)
---------------------------
aaa | 5 | 8
bbb | 3 | 5
and why does sql behaves like that?
To see why, take the group and sums out of your query and look at what it's summing:
SELECT t.type, val, cost
FROM
tbltype t
LEFT JOIN tblcost c ON (c.typeid = t.id)
LEFT JOIN tblvalue v ON (v.typeid = t.id)
You'll see you have each possible combination of the rows from tblcost and tblvalue in the output-- this means some of them get counted multiple times when you sum them.
You need to aggregate tblcost and tblvalue separately. You can then join them back onto tbltype. Gavin's answer already shows one way to do that. Another way is:
SELECT t.type, COALESCE(cost, 0) AS cost, COALESCE(val, 0) AS val
FROM tbltype t
LEFT JOIN (SELECT SUM(cost) AS cost, typeid FROM tblcost GROUP BY typeid) tc
ON tc.typeid = t.id
LEFT JOIN (SELECT SUM(val) AS val, typeid FROM tblvalue GROUP BY typeid) tv
ON tv.typeid = t.id
... which may or may not perform differently (and may or may not be better) depending on which database engine you're actually using.
SELECT t.type,
COALESCE((SELECT SUM(v.val) FROM tblvalue AS v WHERE v.typeid = t.id),0) AS val,
COALESCE((SELECT SUM(c.cost) FROM tblcost AS c WHERE c.typeid = t.id),0) AS cost
FROM tbltype AS t;
I think you've got enough answers suggesting how to solve your problem correctly. You've also got #araqnid's answer that helps you to see why you get such results in the end. The only thing that remains for me seems to be to explain the behaviour itself, as per your request.
Basically, the reason behind such behaviour is the fact that the second join is performed not on tbltype and tblvalue, as one might think, but on the result of the join between tbltype and tblcost, on the one hand, and the tblvalue table, on the other. Now, the first join produces duplicates of t.id, because they match the second table more than once:
tbltype tblcost
id type id cost typeid t.id t.type c.id c.cost c.typeid
-- ---- × -- ---- ------ = ---- ------ ---- ------ --------
1 aaa 1 5 1 1 aaa 1 5 1
1 bbb 2 3 1 1 aaa 2 3 1
3 1 2 2 bbb 3 1 2
4 4 2 2 bbb 4 4 2
The second join produces more duplicates, because:
every occurrence of t.id from the first join's result set is getting matched against v.typeid
and
the typeid values in the tblvalue table are duplicated too.
As a result, rows from both tblcost and tblvalue get duplicated in the process:
tblvalue
t.id t.type c.id c.cost c.typeid id val typeid
---- ------ ---- ------ -------- -- --- ------
1 aaa 1 5 1 × 1 3 1 =
1 aaa 2 3 1 2 2 1
2 bbb 3 1 2 3 2 2
2 bbb 4 4 2 4 1 2
t.id t.type c.id c.cost c.typeid v.id v.val v.typeid
---- ------ ---- ------ -------- ---- ----- --------
1 aaa 1 5 1 1 3 1
1 aaa 1 5 1 2 2 1
= 1 aaa 2 3 1 1 3 1
1 aaa 2 3 1 2 2 1
2 bbb 3 1 2 3 2 2
2 bbb 3 1 2 4 1 2
2 bbb 4 4 2 3 2 2
2 bbb 4 4 2 4 1 2
The only way out for you seems to be to aggregate each table separately. That doesn't necessarily imply separate queries, just separate subqueries, as you can now see from the answers.
QUickest solution is to split the query in two
SELECT
`tbltype`.`type`, SUM(val)
FROM
tbltype
LEFT JOIN `tblvalue` ON (`tblvalue`.`typeid` = `tbltype`.`id`)
GROUP BY `tbltype`.`type`;
and
SELECT
`tbltype`.`type`, SUM(cost)
FROM
tbltype
LEFT JOIN `tblcost` ON (`tblcost`.`typeid` = `tbltype`.`id`)
GROUP BY `tbltype`.`type`;
Something like this:
select t1.id, val, cost from (
select t.id, sum(val) as val
from tbltype t
join tblvalue v on t.id = v.typeId
group by t.id
) t1
join (
select t.id, sum(cost) as cost
from tbltype t
inner join tblcost c on t.id = c.typeid
group by t.id
) t2 on t1.id = t2.id
... or if tblcost and tblvalue are related by id:
select t.id, sum(val) as val, sum(cost) as cost
from tbltype t
inner join tblcost c on t.id = c.typeid
inner join tblvalue v on c.id = v.id
group by t.id