MySQL: How to add depended records to view - mysql

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

Related

Find rows that have different value on a column in MySQL

I want to get all the user_id that are having different user_ip_address on user_device table using a MySQL query.
Table: user_device
id user_id user_ip_address
-----------------------------------
1 1 1.1.1.1
2 1 1.1.1.1
3 2 2.2.2.2
4 3 3.3.3.3
5 1 10.10.10.10
6 4 4.4.4.4
7 2 20.20.20.20
8 3 3.3.3.3
In this data, user_id 1 and user_id 2 have different user_ip_address.
It should output 1 and 2
I tried:
SELECT user_ip_address AS c
FROM user_device WHERE user_ip_address IN (SELECT user_id
FROM user_device group by user_id having user_ip_address!=c)
Use:
select user_id
from user_device
group by user_id
having count(distinct user_ip_address) >1;
Result:
user_id
1
2
Demo
Count the number of different addresses each user has:
SELECT user_id
FROM user_device
GROUP BY user_id
HAVING COUNT(DISTINCT user_ip_address) > 1
untested:
select user_id
from (
select user_id
from user_device
group by user_id, user_ip_address
)
group by user_id
having count(*) > 1
should give you in theory user_id = 1 and 2

Join table two column as 1 column

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

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

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