Mysql Extract max number from a string - mysql

Do you know if there is any manner to extract maximum number into a string in MySql ?
My table looks like this:
+-----------------------------------------------------------+
| Freq |
+-----------------------------------------------------------+
| 40.56 |
| 99.51 |
| 99.87 |
| T=0.00, TCCA=0.00, TTCC=0.03, TTCG=0.00, TTCT=55.0 |
| C=97.70, T=0.05 |
| 44.61 |
| 57.45 |
| 38.18 |
| 38.43 |
| 39.36 |
| 38.65 |
| 39.30 |
| C=57.53, T=0.17 |
| TAAAAAAAAAAA=0.51, TAAAAAAAAAA=68.03, TAAAAAAAAAAAG=31.42 |
+-----------------------------------------------------------+
Desired output:
55.0
97.70

Related

SQL: Detect multiple same entries as a batch

The use-case is a follows. When I have multiple entries with same content, the results should be somehow clubbed together depending on the exact data except some important key.
------------------------------------
| student_id | subject_id | marks |
------------------------------------
| STD001 | SUB001 | 10 |
| STD001 | SUB002 | 20 |
| STD002 | SUB001 | 10 |
| STD002 | SUB002 | 20 |
| STD003 | SUB001 | 11 |
| STD003 | SUB002 | 20 |
| STD004 | SUB001 | 11 |
| STD004 | SUB002 | 20 |
| STD005 | SUB001 | 11 |
| STD005 | SUB002 | 20 |
| STD005 | SUB003 | 30 |
------------------------------------
Expected final result
-------------------------------------------------
| subject_id_str | marks_str | student_id |
-------------------------------------------------
| SUB001_SUB002 | 10_20 | STD001 |
| SUB001_SUB002 | 10_20 | STD002 |
| SUB001_SUB002 | 11_20 | STD003 |
| SUB001_SUB002 | 11_20 | STD004 |
| SUB001_SUB002_SUB003 | 11_20_30 | STD005 |
-------------------------------------------------
OR
--------------------------------------------------------
| subject_id_str | marks_str | student_id_str |
--------------------------------------------------------
| SUB001_SUB002 | 10_20 | STD001_STD002 |
| SUB001_SUB002 | 11_20 | STD003_STD004 |
| SUB001_SUB002_SUB003 | 11_20_30 | STD005 |
--------------------------------------------------------
More Info: Using MySQL 5.7
You can use string aggregation:
select
group_concat(subject_id order by subject_id separator '_') as subject_id_str,
group_concat(marks order by subject_id separator '_') as marks_str,
student_id
from mytable
group by student_id

Mysql table records are displayed in a crooked manner

I have created a table in MySQL but when I display the table, some records are displayed in a crooked manner.
Here's the table displayed:
select * from air_passenger_profile;
+------------+----------+------------+-----------+---------------------------------+---------------+---------------------+
| profile_id | password | first_name | last_name | address | mobile_number | email_id |
+------------+----------+------------+-----------+---------------------------------+---------------+---------------------+
| PFL001 | PFL001 | LATHA | SANKAR | 123 BROAD CROSS ST,CHENNAI-48 | 9876543210 | LATHA#GMAIL.COM |
| PFL002 | PFL002 | ARUN | PRAKASH | 768 2ND STREET,BENGALURU-20 | 8094564243 | ARUN#AOL.COM |
| PFL003 | PFL003 | AMIT | VIKARAM | 43 5TH STREET,KOCHI-84 | 9497996990 | AMIT#AOL.COM |
| PFL004 | PFL004 | AARTHI | RAMESH | 343 6TH STREET,HYDERABAD-76 | 9595652530 | AARTHI#GMAIL.COM |
| PFL005 | PFL005 | SIVA | KUMAR | 125 8TH STREET,CHENNAI-46 | 9884416986 | SIVA#GMAIL.COM |
| PFL006 | PFL006 | RAMESH | BABU | 109 2ND CROSS ST,KOCHI-12 | 9432198760 | RAMESH#GMAIL.COM |
| PFL007 | PFL007 | GAYATHRI | RAGHU | 23 2ND CROSS ST,BENGALURU-12 | 8073245678 | GAYATHRI#GMAIL.COM |
| PFL008 | PFL008 | GANESH | KANNAN | 45 3RD ST,HYDERABAD-21 | 9375237890 | GANESH#GMAIL.COM |
+------------+----------+------------+-----------+---------------------------------+---------------+---------------------+
You place it into your database with spaces. At the point where you insert your variables into the databse, you could use PHP's trim() function, or MySQL's, to store it without the spaces.
To correct your current values:
UPDATE air_passenger_profile SET first_name = TRIM(first_name), etc...

Make sequence autoincrement order number by variable in Mysql

In my example I have this table :
| name | number |
-------------------
| abc | |
| bca | |
| sad | |
| tyu | |
| hjh | |
| lpk | |
| ass | |
| drc | |
| dfg | |
then i get some variable filled with number like :
$order = 3, then i want to make query to update the table above to look like this :
| name | number |
--------------------
| abc | 1 |
| bca | 2 |
| sad | 3 |
| tyu | 1 |
| hjh | 2 |
| lpk | 3 |
| ass | 1 |
| drc | 2 |
| dfg | 3 |
How do I do that in mysql query??
Thanks in advance guys
SET #order=3;
UPDATE Table1 SET number2=MOD(number-1,#order)+1;

Populating a column by extracting a value from other column in the same table

I have a table callInfo and it looks like this:
+----+------------------------------------------------------------------------------------------------------------------------------------+
| id | idUrl | collectionId | |
+----+------------------------------------------------------------------------------------------------------------------------------------+
| 1 | id?books.0.levelOfDetail=high&books.0.shopId=727&books.0.type=books&collectionId=20092014&type=seasonPassSource | |
| 2 | id:call3?books.0.levelOfDetail=high&books.0.shopId=123&books.0.type=books&collectionId=16645&type=seasonPassSource | |
| 3 | id:call3?maxDepth=1&parentMixId=777&type=mixSource | |
| 4 | idSet:call3?keyword=%22FOO%20BAR%20.%5E%24*%2B%3F%7C%28%29%7B%7D%5B%5D%22&type=wishListSource | |
| 5 | idSet:call3?books.0.levelOfDetail=high&books.0.shopId=727 | |
| 6 | idSetSource.0.booksNumber=2&collectionId=16645&books.0.levelOfDetail=high&books.0.type=books&type=seasonPassSource | |
| 7 | idSet:call3?keyword=hero&type=wishListSource | |
+----+-------------------------+----------------------------------------------------------------------------------------------------------+
I have created a new column called collectionId in the table callInfo, but the are many records in the table for which I need to update this column value.
I need to extract the value of collectionId from idUrl column and put it in collectionId column.
it should look like this
+----+------------------------------------------------------------------------------------------------------------------------------------+
| id | idUrl | collectionId | |
+----+------------------------------------------------------------------------------------------------------------------------------------+
| 1 | id?books.0.levelOfDetail=high&books.0.shopId=727&books.0.type=books&collectionId=20092014&type=seasonPassSource | 20092014 |
| 2 | id:call3?books.0.levelOfDetail=high&books.0.shopId=123&books.0.type=books&collectionId=16645&type=seasonPassSource | 16645 |
| 3 | id:call3?maxDepth=1&parentMixId=777&type=mixSource | NULL |
| 4 | idSet:call3?keyword=%22FOO%20BAR%20.%5E%24*%2B%3F%7C%28%29%7B%7D%5B%5D%22&type=wishListSource | NULL |
| 5 | idSet:call3?books.0.levelOfDetail=high&books.0.shopId=727 | NULL |
| 6 | idSetSource.0.booksNumber=2&collectionId=16645&books.0.levelOfDetail=high&books.0.type=books&type=seasonPassSource | 16645 |
| 7 | idSet:call3?keyword=hero&type=wishListSource | NULL |
+----+-------------------------+----------------------------------------------------------------------------------------------------------+
I need to do this in MySQL. Any ideas?
Another way to do it using SUBSTRING_INDEX()
UPDATE callInfo
SET collectionId = SUBSTRING_INDEX(SUBSTR(idUrl, INSTR(idUrl, 'collectionId=') + 13), '&', 1)
WHERE idUrl LIKE '%collectionId=%'
Output:
| ID | ... | COLLECTIONID |
|----| ... |--------------|
| 1 | ... | 20092014 |
| 2 | ... | 16645 |
| 3 | ... | (null) |
| 4 | ... | (null) |
| 5 | ... | (null) |
| 6 | ... | 16645 |
| 7 | ... | (null) |
Here is SQLFiddle demo

how to alter a mysql table to add a column which contains a common value for all rows by checking another columns repeating value

how to alter a mysql table to add a column which contains a common value for all rows by checking another columns repeating value.
+----+----------+--------------+
| id | inode | name |
+----+----------+--------------+
| 1 | 12059010 | IwnsuAaJUFaa |
| 2 | 12059015 | IwnsuAaJUFab |
| 3 | 12059016 | IwnsuAaJUFac |
| 4 | 12059017 | IwnsuAaJUFad |
| 5 | 12059018 | IwnsuAaJUFae |
| 6 | 12059019 | IwnsuAaJUFaf |
| 7 | 12059020 | IwnsuAaJUFag |
| 8 | 12059021 | IwnsuAaJUFah |
| 9 | 12059022 | IwnsuAaJUFai |
| 10 | 12059023 | IwnsuAaJUFaj |
| 11 | 12059013 | iPhZIWtZdSaa |
| 12 | 12059015 | iPhZIWtZdSab |
| 13 | 12059016 | iPhZIWtZdSac |
| 14 | 12059017 | iPhZIWtZdSad |
| 15 | 12059018 | iPhZIWtZdSae |
| 16 | 12059019 | iPhZIWtZdSaf |
| 17 | 12059020 | iPhZIWtZdSag |
| 18 | 12059021 | iPhZIWtZdSah |
| 19 | 12059022 | iPhZIWtZdSai |
| 20 | 12059023 | iPhZIWtZdSaj |
+----+----------+--------------+
i need a fourth column having a common number for all rows group by same "inode" number.
Add the column using something like
ALTER TABLE [tablename] ADD [columname] [columntype]
Change the content of that column using UPDATE.