MySQL explode comma separated string in another table? - mysql

I have table :
id | name
1 | a,b,c
2 | b
in another table i want like this :
id | parent | name
1 | 1 | a
2 | 1 | b
3 | 1 | c
4 | 2 | b
Please help me how to get it done in MySQL

Related

How to generate (join) table field using existing string field as source?

Is it possible to generate (join) a new field by combining existing fields as source ?
I have a table like this:
id | title | parent_id | path
1 | a | 0 | 1
2 | b | 1 | 1/2
3 | c | 1 | 1/3
4 | d | 3 | 1/3/4
I want to use the "path" field's string (numbers) as a kind of index and generate (join) a new field using titles insead ids like:
id | title | parent_id | path | title_path
1 | a | 0 | 1 | a
2 | b | 1 | 1/2 | a/b
3 | c | 1 | 1/3 | a/c
4 | d | 3 | 1/3/4| a/c/d
Is this possible using sql on itself or should I use php for this?

Automatically inserting additional columns in MySQL 8.0 [duplicate]

This question already has an answer here:
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 3 years ago.
Say I have a table like so
+----+----------+------+
| id | name | type |
+----+----------+------+
| 1 | apple | F |
| 1 | pear | F |
| 1 | cucumber | V |
| 2 | orange | F |
| 2 | grass | NULL |
| 2 | broccoli | V |
| 3 | flower | NULL |
| 3 | tomato | NULL |
+----+----------+------+
I want to end up with a table that counts the number of elements for each type (including NULL types) AND for each id, like this:
+----+-----------------+--------------+--------------+
| id | type_NULL_count | type_F_count | type_V_count |
+----+-----------------+--------------+--------------+
| 1 | 0 | 2 | 1 |
| 2 | 1 | 1 | 1 |
| 3 | 2 | 0 | 0 |
+----+-----------------+--------------+--------------+
This is rather easy to do, but is there a way (a query I can write or something else) such that when I go back and edit one of the type fields in the first table, I end up with a properly updated count table?
For example, let's say I want to add a new type (type X) and change the type field for flower from NULL to X. Is there a way to end up with the following table without having to rewrite the query or add more statements?
+----+-----------------+--------------+--------------+--------------+
| id | type_NULL_count | type_F_count | type_V_count | type_X_count |
+----+-----------------+--------------+--------------+--------------+
| 1 | 0 | 2 | 1 | 0 |
| 2 | 1 | 1 | 1 | 0 |
| 3 | 1 | 0 | 0 | 1 |
+----+-----------------+--------------+--------------+--------------+
I'm not sure if this is the best way to do this, so I am open to suggestions
Having a secondary table which it's number of columns changes based on your first table is not a viable option.
Do you need to keep the result in a table or it will be displayed as a report?
I think a better way to do this is using the SQL below calculate counts by id plus type and display using your data display tool the way you like it.
select id, type, count(*) count
from d
group by 1,2
order by 1,2
The output would be
id type count
1 F 2
1 V 1
2 F 1
2 V 1
2 1
3 X 1
3 1

mysql: merge multiple rows into one

I have two tables: state_current and state_snapshots. state_current contains exactly 4 rows, the current values for 4 different keys:
+-----+-------+
| key | value |
+-----+-------+
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
+-----+-------+
Now, I want to add a row to state_snapshots that contains the values of each key in a seperate column:
+---+---+---+---+
| A | B | C | D |
+---+---+---+---+
| 1 | 2 | 3 | 4 |
| 1 | 2 | 3 | 5 |
| 1 | 2 | 4 | 5 |
...
+---+---+---+---+
Of course, the keys never change in state_current, only the values. What mySQL-query will create a row with the value of A in state_current in the first column, the value of B in state_current in the second and so on?
I'm new to mySQL, so thanks in advance for any help!
The simplest answer I can think about is:
insert into state_snapshots(a,b,c,d)
values ( (select value from state_current where key='A'),
(select value from state_current where key='B'),
(select value from state_current where key='C'),
(select value from state_current where key='D')
);

mysql getting data and looking it up in another table

I've got two tables in my database. Table 1 is a list of "timelines" and their corresponding owners and title.
Table 2 is a list of users who have access to the timelines but are followers, not owners.
I'm trying to write a query that outputs the lineID's and corresponding titles that are linked to a userID in either of the two tables.
A query for userID 1 would ideally output:
1 a
2 b
3 c
6 f
Hopefully this isn't too confusing but the purpose is to fill a dynamically generated select box with the LineID and Title for a given UserID...
Table 1 ("owners")
--------------------------
| LineID | UserID | Title |
| 1 | 1 | a |
| 2 | 1 | b |
| 3 | 1 | c |
| 4 | 2 | d |
| 5 | 2 | e |
| 6 | 1 | f |
--------------------------
Table 2 ("followers")
----------------------------
| RowID | LineID | UserID |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 3 | 1 |
| 4 | 3 | 2 |
| 5 | 2 | 2 |
| 6 | 6 | 1 |
----------------------------
I tried using:
SELECT title
FROM `lines`
LEFT JOIN follow
ON follow.user_id = lines.user_id
WHERE follow.user_id = 1
That ended up producing duplicate rows.
The output I need would ideally be an array consisting of all the lineID's and Titles associated with that userID.
select LineId, Title
from owners
where LineId in (select LineId from followers group by LineId )
order by owners.LineId

MySQL: part of table-A join part of table-B

Here is problem I have:
Table A
id_a | name
---------------------
1 | name-A
2 | name-B
3 | name-C
4 | name-D
5 | name-E
6 | spcial_type
Table B
id_b | id_a | condition | subtype
-------------------------------------------------
1 | 2 | 1 | 1
2 | 1 | 0 | 1
3 | 1 | 1 | 2
4 | 2 | 0 | 1
5 | 4 | 0 | 1
6 | 5 | 1 | 1
7 | 2 | 1 | 3
Terms:
Table A: “special_type” exluded
Table A: rows not present in Table B included
Table B: all with condition=0 exluded
Result Table:
id_r | id_a | id_b | name | condition
-----------------------------------------------
1 | 1 | 3 | name-A | 1
2 | 2 | 1 | name-B | 1
3 | 5 | 6 | name-E | 1
4 | 2 | 7 | name-B | 1
5 | 3 | null | name-C | null
table A.subtype is only aux. to show that id_a can be stored many times with condition=1
What I tried:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A
LEFT JOIN Table z ON x. id_a = z. id_a
but this got me items with condition=0, which I do not want
so I tried:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A
LEFT JOIN Table z ON x. id_a = z. id_a where z.condition=1
but that idea excluded items from Table A not present in Table B, and I want these items.
Can it be don inside of MySQL, or do I need scripting lang. to sort it out?
Thoughts anyone?
OK
I must have had a temp. black out.
Here it is:
select x.id_a, x.name, z.id_b, z.id_a, z.condition from Table A
LEFT JOIN Table z ON x. id_a = z. id_a AND z.condition=1
condition below
AND z.condition=1
was the key, when placed in join condition not in where clause