In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
When I study, I like to compare MySQL and Oracle, Python and MySQL, there is always some harvest, but also a new understanding.
While sorting through this section today, I found that Python and MySQL still have a lot in common. Learning a language, a database, and string processing are all relatively important parts, so I decided to compare the two.
The following demo will have Python and MySQL on one side, so it doesn't feel awkward to follow this line of thought.
escape character
>>> print '\\'
\
mysql> select '\\';
+---+
| \ |
+---+
| \ |
+---+
>>> print '\"'
"
mysql> select '\"';
+---+
| " |
+---+
| " |
+---+
>>> print '\''
'
mysql> select '\'';
+---+
| ' |
+---+
| ' |
+---+
string concatenation
>>> x = 'hello'
>>> y = 'tester'
>>> z = x + y
>>> print z
hellotester
set @x='hello';
set @y='tester';
mysql> select @x;
+-------+
| @x |
+-------+
| hello |
mysql> select @y;
+--------+
| @y |
+--------+
| tester |
+--------+
mysql> select concat(@x,@y);
+---------------+
| concat(@x,@y) |
+---------------+
| hellotester |
+---------------+
string copy
>>> print '#'*20
####################
mysql> select repeat('#',20);
+----------------------+
| repeat('#',20) |
+----------------------+
| #################### |
+----------------------+
>>> print ' '*20 + 'end'
end
mysql> select space(20);
+----------------------+
| space(20) |
+----------------------+
| |
+----------------------+
string truncation
>>> name = 'yangjianrong'
>>> name[0]
'y'
>>> name[-1]
'g'
>>> name[1]
'a'
>>> name[1:4]
'ang'
>>> name[:]
'yangjianrong'
>>>
>>> name[1:4:2]
'ag'
mysql> set @name:='yangjianrong';
mysql> select left(@name,1);
+---------------+
| left(@name,1) |
+---------------+
| y |
+---------------+
mysql> select right(@name,1);
+----------------+
| right(@name,1) |
+----------------+
| g |
+----------------+
mysql> select substring(@name,2,3);
+----------------------+
| substring(@name,2,3) |
+----------------------+
| ang |
+----------------------+
mysql> select substring(@name,1);
+--------------------+
| substring(@name,1) |
+--------------------+
| yangjianrong |
+--------------------+
Or use mid
mysql> select mid(@name,2,3);
+----------------+
| mid(@name,2,3) |
+----------------+
| ang |
+----------------+
mysql> select mid(@name,1);
+--------------+
| mid(@name,1) |
+--------------+
| yangjianrong |
+--------------+
>>> name
'yangjianrong'
>>> print '%s' %name
yangjianrong
String formatting, matching
>>> '{name},{alias}'.format(name='yangjianrong',alias='jeanron100')
'yangjianrong,jeanron100'
>>>
mysql> select concat(insert(@name,1,4,'yangjianrong'),insert(@alias,1,5,'jeanron100')) comm;
+------------------------+
| comm |
+------------------------+
| yangjianrongjeanron100 |
+------------------------+
String Length>>> ba
'this is a test bar'
>>> len(ba)
18
mysql> select length(@ba); String Space Handling
>>> s = ' abc '
>>> s.lstrip()
'abc '
>>> s.rstrip()
' abc'
>>> s.strip()
'abc'
>>>
mysql> set @s=' abc ';
Query OK, 0 rows affected (0.00 sec)
mysql> select ltrim(@s);
+-----------+
| ltrim(@s) |
+-----------+
| abc |
+-----------+
1 row in set (0.00 sec)
mysql> select rtrim(@s);
+-----------+
| rtrim(@s) |
+-----------+
| abc |
+-----------+
1 row in set (0.00 sec)
mysql> select trim(@s);
+----------+
| trim(@s) |
+----------+
| abc |
+----------+
1 row in set (0.00 sec)
string matching
>>> l = ['a','b','c']
>>> ''.join(l)
'abc'
>>> '*'.join(l)
'a*b*c'
mysql> select concat_ws(',','a','b','c','d','e') comm;
+-----------+
| comm |
+-----------+
| a,b,c,d,e |
+-----------+
>>> s = 'a b c d e '
>>> s.split(' ')
['a', 'b', 'c', 'd', 'e', '']
mysql> set @s='a b c d e ';
Query OK, 0 rows affected (0.00 sec)
mysql> select replace(@s,' ',',');
+---------------------+
| replace(@s,' ',',') |
+---------------------+
| a,b,c,d,e, |
+---------------------+
string copy
>>> s = 'aabbcc'
>>> s.replace('aa','tt')
'ttbbcc'
mysql> set @s='aabbcc';
Query OK, 0 rows affected (0.00 sec)
mysql> select replace(@s,'aa','tt');
+-----------------------+
| replace(@s,'aa','tt') |
+-----------------------+
| ttbbcc |
+-----------------------+
string encoding
>>> s.encode('utf8')
'aabbcc'
mysql> select convert(@s using utf8);
+------------------------+
| convert(@s using utf8) |
+------------------------+
| aabbcc |
+------------------------+ Determine the character at the beginning of the string match
>>> s.startswith('aa')
True
mysql> SELECT LOCATE('aa',@s,1);
+-------------------+
| LOCATE('aa',@s,1) |
+-------------------+
| 1 |
+-------------------+
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
2. View the Exhibit to examine the description for the SALES table.Which views can have all DML oper
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.