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

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.

Related

Join two tables on nearest matching string

I have a query_table Table and wants to join with match_table Table with nearest matching string. If it was vice-versa then 'like' would have worked but have no idea how to do this.
query_table
+----+------------------+
| id | string |
+----+------------------+
| 1 | fcc9e8796feb |
| 2 | fcdbd7ebcf89 |
| 3 | fccc87896feb |
| 4 | fcc7c7896fef |
| 5 | fcced777aaaf |
+----+------------------+
match_table
+----+-----------+
| id | match_code|
+----+-----------+
| 1 | fcff |
| 2 | fcccc |
| 3 | fccc8 |
| 4 | fccc9 |
| 5 | fccdb |
| 6 | fccdc |
| 7 | fccd8 |
| 8 | fcce |
| 9 | fcced |
| 10 | fccee |
| 11 | fcce6 |
| 12 | fcc7b |
| 13 | fcc7c |
| 14 | fcc8e |
| 15 | fcc87 |
| 16 | fcc88 |
| 17 | fcc9e |
| 18 | fcdbb |
| 19 | fcdbc |
| 20 | fcdbd |
+----+-----------+
I expect
result
+----+------------------+----+----------------+
| id | string | id | match_code |
+----+------------------+----+----------------
| 1 | fcc9e8796feb | 17 | fcc9e |
| 2 | fcdbd7ebcf89 | 20 | fcdbd |
| 3 | fccc87896feb | 3 | fccc8 |
| 4 | fcc7c7896fef | 13 | fcc7c |
| 5 | fcced777aaaf | 9 | fcced |
+----+------------------+----+----------------+

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...

How to query closest date and display the specific data

Hi guys i have a problem displaying a data from my query code.
I want to display a data from the closest date today. Below is my sample data and my output data that i want to display.
Landing Area data
|landing_id| id_number| address |
---------------------------------
| 1 | 00012345 | Ozamiz |
| 2 | 00012346 | Tudela |
| 3 | 00012347 | Nailon |
| 4 | 00012348 | Taboo |
| 5 | 00012349 | Jimenez |
| 6 | 00012350 | Tangub |
---------------------------------
Percentage data
|percent_id| landing_id | percentage | date_added |
-----------------------------------------------------------
| 1 | 1 | 9 |2018-10-08 21:42:22 |
| 2 | 1 | 12 |2018-10-03 20:43:32 |
| 3 | 1 | 43 |2018-10-15 19:43:49 |
| 4 | 3 | 22 |2018-10-10 15:43:56 |
| 5 | 3 | 77 |2018-10-12 18:44:03 |
-----------------------------------------------------------
And this is my query code.
SELECT fish_landing.landing_id,
percentage.landing_id as percentage_landing_id,
percentage.percentage,
percentage.date_added
FROM percentage
RIGHT OUTER JOIN fish_landing ON percentage.landing_id = fish_landing.landing_id
ORDER BY fish_landing.landing_id ASC
The output of my code is this, where the date_added and the percentage is not exact.
|percent_id| percentage | date_added |
----------------------------------------------
| 1 | 9 |2018-10-08 21:42:22 |
| 2 | | |
| 3 | 22 |2018-10-10 15:43:56 |
| 4 | | |
| 5 | | |
| 6 | | |
----------------------------------------------
And the output data that I want to display is the table below, where the percentage that has a latest date_added in every landing_id will be display.
|landing_id| percentage | date_added | address |
--------------------------------------------------------
| 1 | 43 |2018-10-15 19:43:49 | Ozamiz |
| 2 | | | |
| 3 | 77 |2018-10-12 18:44:03 | Nailon |
| 4 | | | |
| 5 | | | |
| 6 | | | |
--------------------------------------------------------
I hope you can help me in my problem.
You can get maximum date_added for a landing_id in a separate derived table and use it for joining.
Try the following:
SELECT fish_landing.landing_id,
percentage.percentage,
percentage.date_added,
fish_landing.address
FROM fish_landing
LEFT JOIN percentage ON percentage.landing_id = fish_landing.landing_id
LEFT JOIN (SELECT landing_id, MAX(date_added) AS max_date_added
FROM percentage
GROUP BY landing_id) AS dt
ON dt.landing_id = percentage.landing_id AND
dt.max_date_added = percentage.date_added
ORDER BY fish_landing.landing_id ASC

Enumerate rows in mysql based on groups with dates in different columns

I have a question very similar to this one, but different (aka, I couldn't extend the answer to that one to fit my purposes. Due to the second WHERE condition, specifically).
I have a table which tracks the visit number for customers. There are two types of visits:
| ID | InStoreVisit | InStoreDate | OnlineVisit | OnlineDate |
|----|--------------|-------------|-------------|------------|
| 1 | 1 | 1/1/11 | | |
| 1 | 2 | 1/2/11 | | |
| 1 | | | 1 | 1/3/11 |
| 1 | 3 | 1/4/11 | | |
| 2 | | | 1 | 2/2/12 |
| 2 | 1 | 2/3/12 | | |
| 2 | | | 2 | 2/4/12 |
I need to create a new column which has a sort of 'global visit number' as such:
| ID | InStoreVisit | InStoreDate | OnlineVisit | OnlineDate | GobalVisit |
|----|--------------|-------------|-------------|------------|------------|
| 1 | 1 | 1/1/11 | | | 1 |
| 1 | 2 | 1/2/11 | | | 2 |
| 1 | | | 1 | 1/3/11 | 3 |
| 1 | 3 | 1/4/11 | | | 4 |
| 2 | | | 1 | 2/2/12 | 1 |
| 2 | 1 | 2/3/12 | | | 2 |
| 2 | | | 2 | 2/4/12 | 3 |
I'm getting mixed up on the WHERE condition with which I can do the self-join. Any advice greatly appreciated.

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