SQL query statements that start with a specified character

Source: Internet
Author: User
Tags mysql tutorial

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.