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
Related
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
MySQL, select non-matching reference or reference does not exist in other table.
For example.
table: category
category_id, category_name
1 XYZ_1
2 XYZ_2
3 XYZ_3
4 XYZ_4
5 XYZ_5
6 XYZ_6
table: type
type_id, type_name
1 A
2 B
3 C
4 D
table: type_match
match_Id, type_id category_id
1 2 1
2 3 1
3 4 1
4 1 1
5 2 2
6 3 2
7 2 3
8 4 3
9 2 4
I need category_id where type_id is not 3 or category_id does not exist in type_match table:
expected result:
category_id
3 // category_id exist in type_match table but not matching type_id = 3
4 // category_id exist in type_match table but not matching type_id = 3
5 // category_id does not exist in type_match table
6 // category_id does not exist in type_match table
Please help.
I tried following answer but not succeed.
How to not select rows that does not exists in other table
You can use a NOT EXISTS query here, checking that there is no row in type_match that has a given category_id and a type_id of 3:
SELECT *
FROM category c
WHERE NOT EXISTS (SELECT *
FROM type_match t
WHERE t.category_id = c.category_id
AND t.type_id = 3
)
Output:
category_id category_name
3 XYZ_3
4 XYZ_4
5 XYZ_5
6 XYZ_6
Demo on dbfiddle
I would probably just use exists logic here:
SELECT c.category_id, c.category_name
FROM category c
WHERE NOT EXISTS (SELECT 1 FROM type_match t
WHERE t.type_id = t.category_id AND t.category_id = c.category_id);
Demo
I have two tables:
goods
id parent_id name
1 null main_part
2 1 add_part1
3 1 add_part2
4 1 add_part3
orders
id good_id count
1 1 5
I want to get a view as :
orders_view
id good_id count
1 1 5
2 2 5
3 3 5
4 4 5
How to do in MySQL?
If your relation is a single level you could try joining the subquery for the union with the orders
select t1.id, t1.id good_id, o.count
from (
select id id_master, id
from goods
where parent_id is null
union all
select parent_id, id
where parent_id is not null
) t
inner join orders_view o on o.good_id = t1.id_master
I have these tables
articles_type
id type
1 article
2 free
3 review
user_fav
uid article_type_id
1 1
1 2
2 1
3 2
3 3
4 1
4 2
4 3
I need to back user with article type if user do not added type then display 0
the expected result
for uid = 1
article_type_id count
1 1
2 1
3 0
------------------------------
for uid = 2
article_type_id count
1 1
2 0
3 0
------------------------------
for uid = 3
article_type_id count
1 0
2 1
3 1
------------------------------
for uid = 4
article_type_id count
1 1
2 1
3 1
I have tried the following query to get the expected result.
QUERY:
select article_type_id, count (article_type_id)
from article_types
left join user_fav on user_fav.article_type_id = article_type.id
where uid = 1
When the condition is in the second table of a left join, then you need to include it the on clause. Also, your query needs a group by:
The query you want is:
select a.article_type_id, count(*)
from article_types a left join
user_fav u
on u.article_type_id = a.id and u.uid = 1
group by a.article_type_id;
select article_type_id, coalesce(count(article_type_id),0)
from article_types
left join user_fav on user_fav.article_type_id = article_type.id
where uid = 1
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