SQL query statements that start with a specified character
For example, many times we have to go to the mysql tutorial and use mssql to query Qualified Data. Today I have encountered a query of all qualified records starting with "Deng.
I used two query methods to view the instance.
First, log in to a database tutorial cc,
Create database cc;
Create a table
Create table if not exists 'string _ find '(
'Id' int (4) not null auto_increment,
'Charlist' varchar (100) default NULL,
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 7;
Import Test Data
Insert into 'string _ find '('id', 'charlist') VALUES
(1, 'Pioneer Deng '),
(2, 'Pioneer Deng '),
(5, 'fdafdsaf '),
(6, 'www. bKjia. c0m ');
Now everything is ready.
$ DbHost = 'localhost'; // It can be unchanged.
$ DbUser = 'root'; // your mysql user name
$ DbPass = 'root'; // change it to your mysql server password
$ Db = 'cc ';
$ Charset = 'gbk ';
$ Conn = mysql_pconnect ($ dbHost, $ dbUser, $ dbPass) or die (mysql_error ());
Mysql_select_db ($ db, $ conn );
Mysql_query ("set Names '$ charset '");
// Query SQL statement 1
$ SQL = "Select * from string_find where charList like 'deng % '";
$ Query = mysql_query ($ SQL );
If (mysql_num_rows ($ query ))
{
While ($ rs = mysql_fetch_array ($ query ))
{
Echo $ rs [1], '<br/> ';
}
}
Else
{
Exit ('no related record found ');
}
/*
Output result
Pioneer Deng
Pioneer Deng
Method query 2
*/
$ SQL = 'select * from string_find where charList REGEXP "^ Deng "';
$ Query = mysql_query ($ SQL );
If (mysql_num_rows ($ query ))
{
While ($ rs = mysql_fetch_array ($ query ))
{
Echo $ rs [1], '<br/> ';
}
}
Else
{
Exit ('no related record found ');
}
/*
The results are different, but the mysql regular expression is used.
This article was originally published on the first stop of www. bKjia. c0m Chinese WEB.