Mysql split comma separated results into multiple rows - mysql

I have a table where two columns are comma separated id's. For example
+--------+-------------+------------+
| name | country_ids | region_ids |
+--------+-------------+------------+
| item 1 | 1,2,3 | 2,4 |
+--------+-------------+------------+
| item 2 | 2,3 | 1,4 |
+--------+-------------+------------+
I would like to run a mysql query so the comma separated values in both columns are split into multiple rows. So in the above example, the results would be
name | country_id | region_id |
+--------+------------+-----------+
item 1 | 1 | 2 |
item 1 | 1 | 4 |
item 1 | 2 | 2 |
item 1 | 2 | 4 |
item 1 | 3 | 2 |
item 1 | 3 | 4 |
item 2 | 2 | 1 |
item 2 | 2 | 4 |
item 2 | 3 | 1 |
item 2 | 3 | 4 |
Is this possible in a mysql statement - even if I have to create a stored procedure on the fly?
THanks

Related

Retrieving hierarchical data from MySQL table

Target platform: MySQL 5.7
I have table categories that represents hierarchical categories data:
+----+-----------------+-----------+
| id | name | parent_id |
+----+-----------------+-----------+
| 1 | First category | NULL |
| 2 | Second category | 1 |
| 3 | Third category | 2 |
| 4 | Other category | 1 |
+----+-----------------+-----------+
Also, I have another table (categories_relations) that stores relations between category and all other related (or connected) categories:
+----+-----------+-------------+
| id | parent_id | relation_id |
+----+-----------+-------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 2 | 2 |
| 6 | 2 | 3 |
| 7 | 3 | 3 |
| 8 | 4 | 4 |
+----+-----------+-------------+
Is it possible to retrieve filtered categories (for example - by ID) with all related other categories? For example, if I would query category with ID=1, query result should be:
+----+-----------------+-----------+
| id | name | parent_id |
+----+-----------------+-----------+
| 1 | First category | NULL |
| 2 | Second category | 1 |
| 3 | Third category | 2 |
| 4 | Other category | 1 |
+----+-----------------+-----------+
If ID=4:
+----+-----------------+-----------+
| id | name | parent_id |
+----+-----------------+-----------+
| 1 | First category | NULL |
| 4 | Other category | 1 |
+----+-----------------+-----------+
And so on. Thank you.

sql count() get wrong result

I have a table like this structure:
| event id | item 1 id | item 2 id | set |
| 1 | 1 | 2 | 1 |
| 1 | 1 | 3 | 1 |
| 1 | 2 | 1 | 1 |
| 1 | 2 | 3 | 1 |
| 1 | 3 | 1 | 1 |
| 1 | 3 | 2 | 1 |
| 1 | 2 | 4 | 2 |
| 1 | 4 | 2 | 2 |
| 2 | 1 | 4 | 3 |
| 2 | 1 | 5 | 3 |
| 2 | 4 | 1 | 3 |
| 2 | 4 | 5 | 3 |
| 2 | 5 | 1 | 3 |
| 2 | 5 | 4 | 3 |
now I wish to count the occurrence of item1 as well as item1 combined with item2 separately
I tried following:
with count_item1 AS (
select event_id, item_1_id, count(distinct set) AS c1 from table
group by event_id, item_1_id
), count_item1_and_item2 AS (
select event_id, item_1_id, item_2_id, count(distinct set) AS c2 from table
group by event_id, item_1_id, item_2_id
)
select t1.event_id, t1.item_1_id, t1.item_2_id, t1.c2, t2.c1
from count_item1_and_item2 AS t1
inner join count_item1 AS t2
on t1.event_id=t2.event_id and t1.item_1_id=t2.item_1_id
for example as table above
so the result for this should be:
| event id | item 1 id | item 2 id | c1 | c2 |
| 1 | 1 | 2 | 1 | 1 |
| 1 | 1 | 3 | 1 | 1 |
| 1 | 2 | 1 | 2 | 1 |
| 1 | 2 | 3 | 2 | 1 |
| 1 | 3 | 1 | 1 | 1 |
| 1 | 3 | 2 | 1 | 1 |
| 1 | 2 | 4 | 2 | 1 |
| 1 | 4 | 2 | 1 | 1 |
| 2 | 1 | 4 | 1 | 1 |
| 2 | 1 | 5 | 1 | 1 |
| 2 | 4 | 1 | 1 | 1 |
| 2 | 4 | 5 | 1 | 1 |
| 2 | 5 | 1 | 1 | 1 |
| 2 | 5 | 4 | 1 | 1 |
the meaning of each row is: in event_id, item 1 appeared c1 in distinct set and (item1, item2) appeared c2 in distinct set.
also count of (item1, item2) is equal to (item2, item1)
then I find a strange thing:
the occurrence of item1 itself should not less than the occurrence of item1 combined with item2, but I find that sometimes item1 combined with item2 has more count than count of item1, is there anything I did wrong here? I think my idea is right but I didn't get the result. I have being stacked in here for a weekend.

How to select recursively select rows from a table matching a certain criteria

We have a Mysql table user_documents. This table maps document with a user.
Data Definition of the table
id - primary key of the table
document_id - foreign key to the document table
user_id - foreign key to the user table.
hierarchy_order - order of hierarchy.
Sample table data
| id | document_id | user_id | hierarchy_order |
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 3 | 1 | 3 | 3 |
| 4 | 1 | 4 | 4 |
| 5 | 2 | 1 | 1 |
| 6 | 2 | 3 | 2 |
| 7 | 2 | 2 | 3 |
| 8 | 2 | 4 | 4 |
| 9 | 3 | 2 | 1 |
| 10 | 3 | 1 | 2 |
| 11 | 3 | 3 | 3 |
| 12 | 3 | 4 | 4 |
| 13 | 4 | 1 | 1 |
| 14 | 4 | 3 | 2 |
| 15 | 4 | 4 | 3 |
| 16 | 4 | 2 | 4 |
We have a hierarchy table list the user in a simple hierarchy. This table is changed frequently and order of users gets changed.
We cannot rely on the hierarchy table as it changes frequently. Hence we are logging the document_id with user_id and what hierarchy order that user was.
For the document_id = 1 in the user_documents table, the hierarchy was.
level 1 (top level) - user_id - 1
level 2 - user_id - 2
level 3 - user_id - 3
level 4 - user_id - 4
Problem Statement
We want to select all records/rows for user_id 2 and other users where their hierarchy order is greater than or equal to user_id 2 hierarchy_order for all document_id.
Expected output
| id | document_id | user_id | hierarchy_order |
| 2 | 1 | 2 | 2 |
| 3 | 1 | 3 | 3 |
| 4 | 1 | 4 | 4 |
| 7 | 2 | 2 | 3 |
| 8 | 2 | 4 | 4 |
| 9 | 3 | 2 | 1 |
| 10 | 3 | 1 | 2 |
| 11 | 3 | 3 | 3 |
| 12 | 3 | 4 | 4 |
| 16 | 4 | 2 | 4 |
We are using MySQL Database and not sure how to achieve this.

Left join IS NULL

Is there a possibility to filter with IS NULL and another variable?
table1.id | table1.product | table2.product_id | table2.attribute_id
1 | Product 1 | 1 | 1
1 | Product 1 | 1 | NULL
1 | Product 1 | 1 | 3
2 | Product 2 | 2 | NULL
2 | Product 2 | 2 | 2
2 | Product 2 | 2 | 3
3 | Product 3 | 3 | 1
3 | Product 3 | 3 | 2
3 | Product 3 | 3 | NULL
So, I would like to filter products with no attribute_id 1 and 2.

How can I merge and count MySQL rows that are identical except for an ID column?

I want to be able to merge rows and find out how many are the same excluding the ID. For example, if I was to have this table:
+---------+-----------+-----------+
| ID | Col 1 | Col 2 |
+---------+-----------+ ----------+
| 1 | 1 | 5 |
| 2 | 1 | 5 |
| 3 | 4 | 9 |
| 4 | 3 | 9 |
| 5 | 1 | 5 |
| 6 | 1 | 5 |
| 7 | 1 | 5 |
| 8 | 4 | 9 |
+---------+-----------+-----------+
It would become:
+---------+-----------+-----------+---------+
| ID | Col 1 | Col 2 | Count |
+---------+-----------+ ----------+---------+
| 1 | 1 | 5 | 5 |
| 2 | 4 | 9 | 2 |
| 3 | 3 | 9 | 1 |
+---------+-----------+-----------+---------+
What would the query for this be?
beside the ID column in your result, to me it looks like you need:
SELECT Col1, Col2, Count(Col1)
FROM myTable
GROUP BY Col1, Col2
select col1,col2,count(*) as `count`
from table
group by col1,col2