MYSQL can't load character into set - mysql

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?

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

listing corresponding data of two tables

i'm pretty new to sql and i have a problem that i coundn't really describe to google
SELECT name AS state
FROM state
WHERE id IN
(
SELECT state_id
FROM city
WHERE id=(SELECT city_id FROM zipcode)
)
;
This lists the states of all zipcodes I have in my database, I also want to list the zipcodes (zipcode.zipcode) in a additional column corresponding with the state
Thank you very much in advance for your help
my tables look like this:
city
+----+----------+-----------+---------------------+---------+---------+
| id | state_id | county_id | name | lat | lng |
+----+----------+-----------+---------------------+---------+---------+
| 1 | 1 | 1 | Prem, Oberbayern | 47.6833 | 10.8 |
| 2 | 2 | 2 | Pfullendorf (Baden) | 47.9249 | 9.25718 |
| 3 | 3 | 3 | Wissen, Sieg | 50.7833 | 7.75 |
| 4 | 1 | 4 | Miltenberg | 49.7039 | 9.26444 |
| 5 | 1 | 5 | Moosthenning | 48.6833 | 12.5 |
| 6 | 1 | 1 | Bernbeuren | 47.7333 | 10.7833 |
| 7 | 4 | 6 | Demmin, Hansestadt | 53.9 | 13.0333 |
| 8 | 2 | 7 | Konstanz, Universit | 47.6667 | 9.18333 |
| 9 | 5 | 8 | Teutschenthal | 51.45 | 11.8 |
| 10 | 6 | 9 | Vierlinden | 52.515 | 14.3141 |
+----+----------+-----------+---------------------+---------+---------+
zipcode
+----+---------+-------------+---------+
| id | city_id | district_id | zipcode |
+----+---------+-------------+---------+
| 1 | 1 | NULL | 86984 |
| 2 | 2 | NULL | 88630 |
| 3 | 3 | NULL | 57537 |
| 4 | 4 | NULL | 63897 |
| 5 | 4312 | 502 | 84164 |
| 6 | 6 | NULL | 86975 |
| 7 | 7 | 778 | 17109 |
| 8 | 8 | NULL | 78462 |
| 9 | 8 | NULL | 78464 |
| 10 | 8 | NULL | 78465 |
+----+---------+-------------+---------+
state
+----+------------------------+
| id | name |
+----+------------------------+
| 1 | Bayern |
| 2 | Baden-W?rttemberg |
| 3 | Rheinland-Pfalz |
| 4 | Mecklenburg-Vorpommern |
| 5 | Sachsen-Anhalt |
| 6 | Brandenburg |
| 7 | Niedersachsen |
| 8 | Schleswig-Holstein |
| 9 | Nordrhein-Westfalen |
| 10 | Th?ringen |
+----+------------------------+
Use explicit JOINs:
SELECT z.*, s.name AS state
FROM zipcode z JOIN
city c
ON z.city_id = c.id JOIN
state s
ON c.state_id = s.id;
If you are learning SQL, focus on learning JOIN rather than IN.

How To select Query

I have a table view named as tes.
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id_oba | int(11) | NO | | NULL | |
| nama_obat | varchar(120) | NO | | NULL | |
| satuan | varchar(20) | YES | | NULL | |
| stok_awal | int(5) | YES | | NULL | |
| persediaan | int(5) | YES | | NULL | |
| nama_suplier | varchar(30) | NO | | NULL | |
| pemakaian | int(4) | NO | | NULL | |
| tanggal | date | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
When I execute:
select id_oba, nama_obat, satuan, stok_awal, sum(pemakaian) AS pemakaian, persediaan,
from tes group by id_oba;
I get this result:
+--------+-----------+--------+-----------+-----------+------------+
| id_oba | nama_obat | satuan | stok_awal | pemakaian | persediaan |
+--------+-----------+--------+-----------+-----------+------------+
| 1 | Paramex | Botol | 30 | 40 | 5 |
| 2 | Oskadon | Botol | 30 | 41 | 27 |
| 3 | Lindon | Botol | 31 | 30 | 4 |
+--------+-----------+--------+-----------+-----------+------------+
How can I modify my query to get the result like this?
+--------+-----------+--------+-----------+-----------+------------+-------+
| id_oba | nama_obat | satuan | stok_awal | pemakaian | persediaan | total |
+--------+-----------+--------+-----------+-----------+------------+-------+
| 1 | Paramex | Botol | 30 | 40 | 5 | 45 |
| 2 | Oskadon | Botol | 30 | 41 | 27 | 67 |
| 3 | Lindon | Botol | 31 | 30 | 4 | 34 |
+--------+-----------+--------+-----------+-----------+------------+-------+
Please help me with my problem.
Thanks.
Change like this
select id_oba, nama_obat, satuan, stok_awal, sum(pemakaian) AS pemakaian,
persediaan,(sum(pemakaian)+persediaan) as total from tes group by id_oba;

How to query for matching record sets

I am trying to do a query to find people living at the same address. But my SQL appears to fail and not sure why.
mysql> SELECT * FROM polished1 WHERE LName = 'BENDICH'; +------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
| id | StateVoterID | Title | FName | MName | LName | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStPreDirection | RegStName | RegStType | RegStPostDirection | RegUnitType | RegUnitNum | RegCity | RegState | RegZipCode | CountyCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted | StatusCode |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
| 2790 | 72253 | | ARNOLD | J | BENDICH | | 5 | M | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL |
| 2791 | 72253 | | JUDITH | E | BENDICH | | 1 | F | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
2 rows in set (0.00 sec)
mysql> SELECT *, COUNT(*) c FROM polished1 WHERE LName = 'Bendich' GROUP BY RegStNum, RegStName, RegStType, RegUnitType, RegStPreDirection, RegStPostDirection, RegUnitNum, RegCity, RegState, RegZipCode HAVING c > 1;
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
| id | StateVoterID | Title | FName | MName | LName | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStPreDirection | RegStName | RegStType | RegStPostDirection | RegUnitType | RegUnitNum | RegCity | RegState | RegZipCode | CountyCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted | StatusCode | c |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
| 2790 | 72253 | | ARNOLD | J | BENDICH | | 5 | M | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL | 2 |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
1 row in set (0.01 sec)
http://bpaste.net/show/UWC8wdOWMbFEQcT7eV6f/
select p.*
from polished1 p JOIN
(
SELECT *, COUNT(*) c
FROM polished1
WHERE LName = 'Bendich'
GROUP BY RegStNum, RegStName, RegStType, RegUnitType, RegStPreDirection, RegStPostDirection, RegUnitNum, RegCity, RegState, RegZipCode
HAVING c > 1) filtered ON p.RegStNum=filtered.RegStNum
and p.RegStName=filtered.RegStName
and p.RegStType=filtered.RegStType
and p.RegUnitType=filtered.RegUnitType
and p.RegStPreDirection=filtered.RegStPreDirection
and p.RegStPostDirection=filtered.RegStPostDirection
and p.RegUnitNum=filtered.RegUnitNum
and p.RegCity=filtered.RegCity
and p.RegState=filtered.RegState
and p.RegZipCode=filtered.RegZipCode
You should retrieve the addresses where count(*)>1 first and then get all joined addresses

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