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

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

Related

MYSQL can't load character into set

I have this file:
1.nothing
2.o,s,f,d
3.f,d
4.o,s
5.s,f,d
6.s
7.nothing
8.s,f,d
9.o,d
10.s,f
And a table:
describe delete_me;
+------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| privileges | set('o','s','f','d') | YES | | NULL | |
+------------+----------------------+------+-----+---------+-------+
When I try to:
LOAD DATA INFILE 'privileges.txt' INTO TABLE delete_me FIELDS TERMINATED BY '.';
I get this:
+------+------------+
| id | privileges |
+------+------------+
| 0 | |
| 2 | f,s,o |
| 3 | f |
| 4 | o |
| 5 | f,s |
| 6 | |
| 7 | |
| 8 | f,s |
| 9 | o |
| 10 | s |
| 11 | o |
| 12 | |
| 13 | o |
| 14 | o |
| 15 | s |
| 16 | |
| 17 | o |
| 18 | o |
| 19 | s,o |
| 20 | f |
+------+------------+
The letter d just disappears. Why?

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;

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.

How to join 3 tables in order to get a result with two tables alternating

Say I have 3 tables, foo bar and baz, where bar and baz having different additional informations about the datasets in foo. Is there a way to join these 3 tables together, so that in every output row is either a (foo <join> bar) or (foo <join> baz) dataset?
Say, I have the following tables:
mysql> select * from foo;
+----+-------+
| id | foo |
+----+-------+
| 1 | start |
| 2 | mid |
| 3 | end |
+----+-------+
mysql> select * from bar;
+----+-----+-----------+
| id | bid | bar |
+----+-----+-----------+
| 1 | 1 | bar-start |
| 2 | 2 | bar-mid |
| 3 | 3 | bar-end |
+----+-----+-----------+
mysql> select * from baz;
+----+-----+-------------+
| id | bid | baz |
+----+-----+-------------+
| 1 | 1 | baz-start-1 |
| 1 | 2 | baz-start-2 |
| 1 | 3 | baz-start-3 |
| 2 | 4 | baz-mid-1 |
| 2 | 5 | baz-mid-2 |
| 2 | 6 | baz-mid-3 |
| 3 | 7 | baz-end-1 |
| 3 | 8 | baz-end-2 |
| 3 | 9 | baz-end-3 |
+----+-----+-------------+
I can extract all the information with a query which joins those tables together via
mysql> select * from foo join bar join baz on foo.id = bar.id and foo.id=baz.id;
+----+-------+----+-----+-----------+----+-----+-------------+
| id | foo | id | bid | bar | id | bid | baz |
+----+-------+----+-----+-----------+----+-----+-------------+
| 1 | start | 1 | 1 | bar-start | 1 | 1 | baz-start-1 |
| 1 | start | 1 | 1 | bar-start | 1 | 2 | baz-start-2 |
| 1 | start | 1 | 1 | bar-start | 1 | 3 | baz-start-3 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 4 | baz-mid-1 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 5 | baz-mid-2 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 6 | baz-mid-3 |
| 3 | end | 3 | 3 | bar-end | 3 | 7 | baz-end-1 |
| 3 | end | 3 | 3 | bar-end | 3 | 8 | baz-end-2 |
| 3 | end | 3 | 3 | bar-end | 3 | 9 | baz-end-3 |
+----+-------+----+-----+-----------+----+-----+-------------+
But I would like to get an output like the following table, since that would make the code in the application consuming the data much simpler:
+----+-------+------+------+-----------+------+------+-------------+
| id | foo | id | bid | bar | id | bid | baz |
+----+-------+------+------+-----------+------+------+-------------+
| 1 | start | 1 | 1 | bar-start | NULL | NULL | NULL |
| 1 | start | NULL | NULL | | 1 | 1 | baz-start-1 |
| 1 | start | NULL | NULL | | 1 | 2 | baz-start-2 |
| 1 | start | NULL | NULL | | 1 | 3 | baz-start-3 |
| 2 | mid | 2 | 2 | bar-mid | NULL | NULL | NULL |
| 2 | mid | NULL | NULL | | 2 | 4 | baz-mid-1 |
| 2 | mid | NULL | NULL | | 2 | 5 | baz-mid-2 |
| 2 | mid | NULL | NULL | | 2 | 6 | baz-mid-3 |
| 3 | end | 3 | 3 | bar-end | NULL | NULL | NULL |
| 3 | end | NULL | NULL | | 3 | 7 | baz-end-1 |
| 3 | end | NULL | NULL | | 3 | 8 | baz-end-2 |
| 3 | end | NULL | NULL | | 3 | 9 | baz-end-3 |
+----+-------+------+------+-----------+------+------+-------------+
Is there a way to convince mysql to give me a result set like the last one?
(
select foo.id as foo_id, foo.foo, bar.id, bar.bid, bar.bar, null, null as baz_bid, null as baz from foo
join bar on foo.id=bar.id
)
union
(
select foo.id as foo_id, foo.foo, null, null, '', baz.id, baz.bid as baz_bid, baz.baz from foo
join baz on foo.id=baz.id
)
order by foo_id asc, bar desc, baz_bid asc

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.