Auto fill other column in mysql table - mysql

I have column in mysql table like this:
| Column A | Column B |
| A | |
| B | |
| C | |
| D | |
| E | |
I want to auto fill the other column using rule if A,C,D then fill X and if B, E fill the Y in the column B field.
The result will be like this:
| Column A | Column B |
|----------|----------|
| A | X |
| B | Y |
| C | X |
| D | X |
| E | Y |
Is there are an easy way to do that in MySQL query?
Thank you for help.

UPDATE table
SET B = CASE
WHEN A IN ('A','C','D') THEN 'X'
WHEN A IN ('B','E') THEN 'Y'
END

Related

How to write SQL for finding columns with matching string

I do have a following table
TableA
| Column A | Column B |
| -------- | -------- |
| 1234 | Y |
| 2345 | N |
| 3456 | Y |
| 3456 | Y |
| 3456 | N |
| 2345 | N |
| 1234 | N |
| 2345 | N |
Here, '1234' and '3456' has values Y and N whereas 2345 has only value N
I want to display values of column A where there are 2 values (Y and N) in Column B. Ideally
| Column A | Column B |
| -------- | -------- |
| 1234 | Y |
| 1234 | N |
| 3456 | Y |
| 3456 | N |
I tried using
Select * from TableA
where column b = 'Y'
and column b = 'N'
but it doesn't give desired result.
I prefer aggregation here:
SELECT ColumnA
FROM TableA
GROUP BY ColumnA
HAVING MIN(ColumnB) <> MAX(ColumnB);
The above assumes that the only two values which would ever appear in ColumnB are Y and N.

Transitive-Closure Table Restructure

How would I be able to retrieve a full-tree from the current structure, or refactor the current table structure to allow for an optimized recursive query?
Issue
Unable to retrieve full-tree of components from base component without iteration.
A single component can have an undefined number of connections (depth).
Components do not have a parent property, as each component can be related to multiple components.
Unable to recursively update affected attribute values of a component.
For example if the price of a component changes, the price is updated for all related components_of.
Current Structure
component
primary key (id)
| id | price |
|----|------ |
| A | 1 |
| B | 1 |
| C | 1 |
| D | 2 |
| E | 2 |
component_closure
unique index (component, component_of)
index (component_of)
FK (component) References component (id)
FK (component_of) References component (id)
| component | component_of |
|--------------------------|
| D | B |
| D | C |
| B | A |
| E | C |
| E | A |
Resulting Graph Model:
Example query:
UPDATE component
SET price = 2
WHERE id = 'A';
Desired Result (* indicates recursively updated values)
| id | price |
|----|------ |
| A | 2 |
| B | 2 | *
| C | 1 |
| D | 3 | *
| E | 3 | *
I am thinking I would need to store the entire tree relationship in the component_closure table, so that I would be able to retrieve the component_of relationships of all components and use a depth column to determine the order of components. Though that seems wasteful when the full-tree is not needed, such as for immediate components_of.
For example:
| component | component_of | depth |
|-----------|--------------|-------|
| D | A | 1 |
| D | B | 2 |
| D | C | 1 |
Yes, if you want to store the transitive closure, you need to store all paths.
For some operations, it's even helpful to store the path of length 0:
| component | component_of | depth |
|-----------|--------------|-------|
| D | D | 0 |
| D | A | 1 |
| D | B | 2 |
| C | C | 0 |
| B | B | 0 |
| B | A | 1 |
| A | A | 0 |
In MySQL 8.0, none of this will be needed. We'll finally be able to use recursive queries.

how to group selection from other table as one column in sql?

here is two tables:
a:
+-----+------------------------+
| id | conten |
+-----+------------------------+
| 1 | q. |
| 2 | q. |
| 3 | s. |
| 4 | g |
| 1 | a |
| 2 | a |
+-----+------------------------+
b:
+-----+------------------------+
| id | type |
+-----+------------------------+
| 1 | I. |
| 2 | II. |
| 3 | III. |
| 4 | IV |
| 5 | V |
| 6 | VI |
+-----+------------------------+
Is there a way to select from a and b so that for one id 2, there will be one additional field that groups all content from that id? the select result should be something like this:
+-----+------------------------+-----------+
| id | type | contents |
+-----+------------------------+-----------+
| 2 |I. | q,a |
+-----+------------------------+-----------+
Edited
btw, if there is a way to do it by sqlahcmey, that would be sweet.
SELECT b.id, b.type, IFNULL(GROUP_CONCAT(a.conten), '') AS contents
FROM b
LEFT JOIN a ON a.id = b.id
GROUP BY b.id
See How do I write a group_concat function in sqlalchemy? for how to translate GROUP_CONCAT to SQLAlchemy.

MySQL multiples rows to columns [duplicate]

This question already has an answer here:
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 5 years ago.
https://i.stack.imgur.com/5Pw2L.png
I have a problem that I would like to solve for pre-processing in MySQL. I have a select that returns multiple rows and columns for an id. I would like to transform the rows into columns for the same id, grouping them as per the attached figure. The column names are not important to me because I only need the values for each id.
+---+---+---+---+-----+---+
| 1 | a | b | c | ... | x |
+---+---+---+---+-----+---+
| 1 | d | e | f | ... | y |
+---+---+---+---+-----+---+
| 2 | g | h | i | ... | z |
+---+---+---+---+-----+---+
| 2 | j | k | l | ... | q |
+---+---+---+---+-----+---+
| 3 | m | n | o | ... | w |
+---+---+---+---+-----+---+
| 3 | p | q | r | ... | t |
+---+---+---+---+-----+---+
+---+---+---+---+-----+---+---+---+---+-----+---+
| 1 | a | b | c | ... | x | d | e | f | ... | y |
+---+---+---+---+-----+---+---+---+---+-----+---+
| 2 | g | h | i | ... | z | j | k | l | ... | q |
+---+---+---+---+-----+---+---+---+---+-----+---+
| 3 | m | n | o | ... | w | p | q | r | ... | t |
+---+---+---+---+-----+---+---+---+---+-----+---+
Unfortunately there is no way to create columns like that on the fly, if you have a variable amount of id repetitions in a table.
You could use group concat to get the same columns into one comma separated column
Select Id, Group_Concat(Col1) As Col1,
Group_Concat(Col2) As Col2,
Group_Concat(Col3) As Col3, ...
Group_Concat(Coln) As Coln
From table
Group By Id

Select rows where column b is found in column a

I have a table that looks like this...
------------------------------------
| sku | superseded_sku |
------------------------------------
| PartA | PartC |
| PartB | PartC |
| PartC | PartD |
| PartD | |
I need to write a query that will show me all rows where an entry from coumn B appears in column A. For example here it would give me the following...
------------------------------------
| sku | superseded_sku |
------------------------------------
| PartC | PartD |
| PartD | |
I have tried this...
SELECT x.sku, x.superseded_sku FROM table x
JOIN table y ON y.sku = x.sku
WHERE y.superseded_sku = x.sku
but it returns nothing and now I don't know where to go from here
Think this query will give you the result:
SELECT x.sku, x.superseded_sku FROM table x
JOIN table y ON y.sku = x.superseded_sku