Update group by elements using sequence - mysql

how can i do something like this:
I have two tables, A and B. A --> B is one to many.
B has a varchar field that we will call field1.
I have also a sequence seq1 that i want to use in this way:
Suppose we have A1 and A2, two records belonging to table A. A1 has (B1,B2,B3,B4) and A2 has (B5,B6).
I want to use the sequence for each group and start from the beginning each time i change group to update field1 in B. So i will have somthing like
B1.field1 = 1, B2.field1 = 2, B3.field1 = 3, B4.field1 = 4
now the sequence start back from 1 for A2:
B5.field1 = 1, B6.field2 = 2.
Is there some complex nested structure to do that or i need a function?
I was thinking about using a temporary table ,playing a bit on indexes and sub countings but i don't manage to find a wayout.
Thanks

Related

How do you update a specific table column based on the value of another column of the same table?

Suppose I have 4 columns: A, B, C, D and one additional column: Index in a row.
Suppose I want to save a value of "50" but depending on the "Index" value of the row mysql will save 50 in either A, B, C, D. Say if Index=1 for that particular row, then 50 goes to column "A".
Is there a mysql query that will accomplish this all in one go? Or do I have to first read the index value, then make a switch statement with four different update queries to accomplish this?
Here is one way to do it:
UPDATE mytable
SET
A = CASE WHEN index = 1 THEN 50 ELSE A END,
B = CASE WHEN index = 2 THEN 50 ELSE B END,
C = CASE WHEN index = 3 THEN 50 ELSE C END,
D = CASE WHEN index = 4 THEN 50 ELSE D END
WHERE ...
The query works by doing conditional value assignment to each column, according to the value of index. When a column does not need to be updated, its original value is simply reassigned, hence turning the operation to a no-op.

Select from table, update the rows selected

I am trying to write a query that selects values from certain rows based on their parameters, then does some calculations and returns the result. I also need to update the rows that were selected. I can only think of ways to do both of these actions with separate queries, but is there a way to do both at once?
Example queries would be:
SELECT SUM(a.val1*b.val1)
FROM a, b
WHERE a.val2 = condition1 AND b.val2 = condition2;
UPDATE a
SET a.val3 = a.val1*b.val1
FROM a INNER JOIN b ON a.val2 = condition1 AND b.val2 = condition2;
Is there a way to combine them?
No. There is no syntax in SQL that allows you to retrieve values and update them in the same query. Use SELECT to retrieve values and UPDATE to change them.
You can use SELECT inside an UPDATE statement as part of the calucalation, but the result will not be the values of what has been updated, only the number of rows that were updated.
Your SELECT statement has a small mistake in it and should be as follows:
SELECT SUM(a.val1*b.val1) FROM a, b WHERE a.val2 = 1 and b.val2 = 1;
This only returns one row: the sum of the product of val1 columns in tables a and b where the val2 columns meet certain conditions.
It is not clear what you are trying to achieve by updating table a with the result of this. If you are looking to set val3 in table a with the product of val1 in tables a and b if the val2 in those tables meet certain criteria, the following might work, but you need to add a join between columns in both tables otherwise val3 will be set to the product of val1 in table a and all the val1 values in table b, which may not be what you want.
UPDATE a
SET a.val3 =
(
SELECT a.val1*b.val1
FROM b
WHERE b.key = a.key
AND b.val2 = condition2
)
WHERE a.val2 = condition1;
Nope :)
In SQL it is always different queries. You can write a function that will do 2 actions, but never one query.
This will have to be done in two steps - Select and Aggregate then Update.

Autogenerate numeric columns based on another column's values

Let's say I have a table with two columns, one column is "State" and the other is "State Number".
I would need to have the column "State Number" have numeric values based on the values of the column "State", alphabetically.
example...
State = A is equals to State Number = 1
State = B is equals to State Number = 2
State = C is equals to State Number = 3
So and and so forth.
Any help would be appreciated.
Thanks!
NO, you can't do it automatically other than defining State Number to be auto_increment. What you are asking can be done using trigger but MySQL doesn't support recursive trigger and thus it's not an option here.
You can perform an UPDATE though using CASE condition and update the said column like
update tbl1
set `State Number` = case when state = 'A' then 1
when state = 'B' then 2
when state = 'C' then 3 end;

How can I implement a query that return a single record containing the informations contained in 2 specific record?

I am not so into SQL and I have the following problem.
So basically I have a table containing the following fields:
id: BigInt
geographical_position: Geometry
// SOME OTHER FIELDS IRRILEVANT FOR MY QUERY
I want select a single record containg the geographical_position values of 2 specific record (have to be presented on the same record), so I try to do something like this:
SELECT
a.geographical_position AS point1,
a.geographical_position AS point2
FROM accomodation a
where a.id = 31 and a.id = 32
But it seems to be wrong because obtain an empty result set.
Basically I want to obtain the geographical_position of the record having id=31 in a cell named point1 and the geographical_position of the record having id=32 in a cell named point2.
How can I do? What is wrong in my query? How can I fix it?
I think you can do it with a self join.
SELECT
a1.geographical_position AS point1,
a2.geographical_position AS point2
FROM accomodation a1
JOIN accomodation a2 ON a1.id=31 and a2.id=32
The query you have currently is wrong as the id can not have 2 values at the same time.
Specify the table twice and reference accordingly:
SELECT
a.geographical_position AS point1,
b.geographical_position AS point2
FROM accomodation a, accomodation b
where a.id = 31 and b.id = 32

How to add more string data to an existing (filled) column

e.g.
Table A (id - auto increment)
id labels names
1 a1,a3,b4 a1
2 a2,b5 a2
3 a1,b4 a2
What i want is to update names column such as "existing_names_data + ', B'" where labels like %b4%
I know following query works with integers not with strings, but i tried and failed anyways -
update TableA set names=names+ " B" where labels like '%b4%'
Is there any such query for strings?
Or, what should I do to get desired output?
Desired Output
id labels names
1 a1,a3,b4 a1, B
2 a2,b5 a2
3 a1,b4 a2, B
Thanks. Regards,
Use the CONCAT() function:
UPDATE foo SET bar = CONCAT(bar, ' B') WHERE foobar = 'barfoo';
..that said, you should really just use a different relational table and normalize your data rather than add comma/semicolon/space seperated columns.
Consider the following structure:
messages:
id
name
message_labels:
id
message_id (JOIN message_labels ON message_labels.message_id = message.id)
label
message_names:
id
message_id (JOIN message_names ON message_names.message_id = message.id)
name
For Strings you may use the || to concat strings:
update TableA set names=names ||' '|| 'B' where labels like '%b4%'