mysql: merge multiple rows into one - mysql

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')
);

Related

Update MySQL table with smallest value from another table

I have been looking around quite a lot but I can't seem to find a solution to this problem.
I got two tables:
|---------------------|-------------------|
| ID | Value |
|---------------------|-------------------|
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
|---------------------|-------------------|
...
|---------------------|-------------------|
| ID | Value |
|---------------------|-------------------|
| 1 | 7 |
| 1 | 18 |
| 2 | 21 |
| 2 | 2 |
| 4 | 103 |
|---------------------|-------------------|
...
Basically what I wanna do is update the NULL-fields from the first table with the smallest value from the second table where there are matching IDs.
So that in the end it looks something like this:
|---------------------|-------------------|
| ID | Value |
|---------------------|-------------------|
| 1 | 7 |
| 2 | 2 |
| 3 | NULL |
| 4 | 103 |
|---------------------|-------------------|
...
I tired out a bunch of things but failed. Can anyone help me?
You could use a sub query:
update t1
inner join (select ID, min(Value) as minimum from t2 group by ID) tempt2 on t1.ID=tempt2.ID
set t1.value=tempt2.minimum;
Basically, you're looking up that minimum value in the second table for each ID, you call that table tempt2, and you join on that.

Mysql : Insert missing rows in a table based on another column value in same table

I'm trying to insert new rows into a table where a value is missing based on the value in another column. So for example in the following table I want add a new row with value 2 for each ref where it is missing.
+----+-------+-----+
| id | value | ref |
+----+-------+-----+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 7 | 1 |
| 4 | 1 | 2 |
| 5 | 7 | 2 |
| 6 | 1 | 3 |
| 7 | 7 | 3 |
| 8 | 8 | 3 |
+----+-------+-----+
So in the above I would get the additional 2 rows
+----+-------+-----+
| 9 | 2 | 2 |
| 10 | 2 | 3 |
+----+-------+-----+
This is probably quite basic, but so is my SQL.
Any help appreciated.
Just to clarify the question,
in the example I am adding the value 2 for all refs which don't have it. So those would be the additional rows, as ref 1 already has value 2. If for example I wanted to add the value 5 for all refs, this would add 3 new rows.
The following seems to work. Thought could do it using just the one table. Turns out ref is a primary key in another table, so this seems to do it.
INSERT INTO ValeRefTable(value , ref)
SELECT 2, RT.id
FROM RefTable RT
WHERE RT.id NOT IN
(
SELECT VRT.ref FROM ValueRefTable VRT WHERE VRT.value = 2
)

Mysql why 2th column isn't sorted from 1 to 3?

Why ordering isn't working right for 2th column. Can some one explain, please.
select a,b from d:
+------+------+
| a | b |
+------+------+
| 1 | 3 |
| 1 | 3 |
| 2 | 1 |
| 2 | 1 |
| 3 | 2 |
| 3 | 2 |
| 3 | 2 |
+------+------+
select a,b from d order by a,b;
+------+------+
| a | b |
+------+------+
| 1 | 3 |
| 1 | 3 |
| 2 | 1 |
| 2 | 1 |
| 3 | 2 |
| 3 | 2 |
| 3 | 2 |
+------+------+
I think it's ordering correctly. In your order by you have asked system to order by First column first so it have ordered then you have asked it to order by second column so it have
1. It have to keep ordering of first column.
2. Order by second column too
So it does ordering within group means if ..
Table Test
A| B
--------
1 1
1 3
1 2
Select * from test order by A, B
Output
A | B
1 1
1 2
1 3
Hope this clears your doubt.
Each record/row in the output has to be consistent. When you use order by, you are printing record/row which is sorted on the basis of specific column value. You can't sort individual column by breaking the consistency of of a row. Otherwise it will create a havoc, imagine something like 'select bank_account_id, balance from bank_record order by bank_account_id, balance;'. What do you think would happen if bank_account_id and balance is sorted individually?

Mysql, How to group a set of rows by using the max value of another column?

I have a table that might have one or more rows with the same value for the key_property column.
I want to build a query in MySql that returns a set where every value of key_property is represented for only one of its corresponding rows and that it also receives a key_property filter for a like sentence, chosen by the max value of another column (say, event_id).
How can I achieve that?
UPDATE:
Here's a sample of the non-filtered table:
+--------------+--------+----------+
| key_property | others | event_id |
+--------------+--------+----------+
| abcd | B | 1 |
| abcd | A | 2 |
| defg | C | 3 |
| abcd | D | 4 |
| hijk | f | 4 |
+--------------+--------+----------+
When executing the query with the filter setted as 'd', the result data should look like this:
+--------------+--------+----------+
| key_property | others | event_id |
+--------------+--------+----------+
| abcd | D | 4 |
| defg | C | 3 |
+--------------+--------+----------+
SELECT *
FROM tab WHERE (key_property,event_id) IN
( SELECT key_property, MAX(event_id)
FROM tab
WHERE key_property like '%d%'
GROUP BY key_property
)

mysql select to give all rows of one column that match any row of another?

+-----------------+-----------+-------+-------------+
| orders_total_id | orders_id | value | class |
+-----------------+-----------+-------+-------------+
| 1 | 1 | 34.00 | ot_subtotal |
| 2 | 1 | 8.56 | ot_shipping |
| 3 | 1 | 2.38 | ot_tax |
| 4 | 1 | 0.600 | ot_tax |
| 5 | 2 | 45.54 | ot_subtotal |
| 6 | 2 | 8.56 | ot_shipping |
+-----------------+-----------+-------+-------------+
I want to show ALL the records that belong to ANY rows which also have a row that has class=ot_tax. So in the above, rows 1-4 would match because one (or more) of them have a class of ot_tax and they all have the same orders_id, but NOT rows 5-6 because they also have same orders_id but none have class=ot_tax.
I want to say the key here has to do with grouping, but SQL syntax still feels so foreign to me. Thanks.
So you want to get all orders that have a row with class='ot_tax' ?
There are several ways, for instance using the in predicate:
SELECT * FROM table_name
WHERE orders_id IN (SELECT orders_id FROM table_name WHERE class='ot_tax')