Wu Yuxiong - natural born MySQL study notes: MySQL LIKE clause

Use SQL SELECT command to read the data in MySQL, we can use a WHERE clause in the SELECT statement to get the specified record. 
WHERE clause can use the equals sign = the setting condition to acquire data, such as " runoob_author = 'RUNOOB.COM' " . 
But sometimes need to get runoob_author field contains " COM " all records characters, then you need to use SQL LIKE clauses in the WHERE clause. 
SQL LIKE clause using percent % character to represent any character, similar to the UNIX regular expression or asterisk * . 
Without using percent %, LIKE clauses and the = the effect is the same. 
Syntax 
The following SQL SELECT statement is used to read the LIKE clause common syntax data from the data table: 
the SELECT field1, Field2, ... fieldN 
the FROM table_name 
WHERE field1 LIKE condition1 [the AND [OR]] filed2 = ' someValue '  
may WHERE clause specifying any conditions.
may WHERE clause LIKE clause.
LIKE clause may be used in place of the equals sign = . 
LIKE usually associated with % used together, similar to the search for a meta-characters. 
AND or OR may be used to specify one or more conditions. 
You can use WHERE ... LIKE clause to specify the conditions in DELETE, or UPDATE command.
Use the command prompt LIKE clause 
will use the SQL SELECT command WHERE ... LIKE clause to read data from MySQL in the data table runoob_tbl. 
The following are all records to COM end of the table acquired runoob_author runoob_tbl field: 
MySQL > use RUNOOB; 
Database changed 
MySQL > the SELECT * from runoob_tbl the WHERE runoob_author the LIKE ' % COM ' ;
PHP script used in the LIKE clause 
can use PHP function mysqli_query () and put on the same SQL SELECT WHERE ... LIKE clause command to get the data. 
This function is used to execute SQL command, and then () outputs the data in all queries by PHP function mysqli_fetch_array. 
But if it is used in DELETE, or UPDATE WHERE ... LIKE clause S QL statement, you do not need to use mysqli_fetch_array () function. 
Here is a PHP script to read all the records to COM at the end of the runoob_author field in runoob_tbl table:
 < PHP? 
$ Dbhost = ' localhost: 3306 ' ; // MySQL server host address 
$ dbuser = ' root ' ; // mysql username 
$ dbpass = ' 123456 ' ; // mysql username and password 
$ conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('连接失败: ' . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
$sql = 'SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl
        WHERE runoob_author LIKE "%COM"';
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('无法读取数据: ' . mysqli_error($conn));
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row['runoob_id']}</td> ".
         "<td>{$row['runoob_title']} </td> ".
         "<td>{$row['runoob_author']} </td> ".
         "<td>{$row['submission_date']} </td> ".
         "</tr>";
}
echo '</table>';
mysqli_close($conn);
?>

 

Guess you like

Origin www.cnblogs.com/tszr/p/12113763.html