如何选取匹配指定条件的MYSQL数据?

| | Comments (0)



指定条件选取mysql数据库中的条件经常用到,在编写程序过程中,用到mysql就写入和读取两部操作,其实mysql的应用不难,只是没有上手前觉得很困难,任何程度代码的编码基础代码是一样的,在实际应用中就是这些基础代码的组合和逻辑上的判断加之反复调用。

选取MYSQL匹配指定条件的数据, 需要用到SELECT 语句和WHERE 子句,在php中执行SELECT 语句和WHERE 子句则需要mysql_query() 函数,这样就可以实现我们的目的了。

范例:

下面的例子将从 "Person" 表中选取所有 FirstName='Peter' 的行:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM person
WHERE FirstName='Peter'");

while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
  }

?>

以上代码的输出:

Peter Griffin
 
 

where语句相当于if假设的意思,平时比如excel中经常会用if来判断条件,如果遇到文本一般是等于或者不等于,而如果是数字还可以小于或者大于等等。

下面的运算符可与 WHERE 子句一起使用:

运算符 说明
= 等于
!= 不等于
> 大于
< 小于
>= 大于或等于
<= 小于或等于
BETWEEN 介于一个包含范围内
LIKE 搜索匹配的模式

 

 

更多的关于php mysql的运用请参考:

 

http://www.w3school.com.cn/php/php_mysql_where.asp(中文)

 

http://www.w3schools.com/php/php_mysql_intro.asp(英文)

 

单点日志 http://spoint.babyshoot.cn

Leave a comment

Archives

Ads by google

Pages

Powered by Movable Type 4.24-en

About this Entry

This page contains a single entry by 单点日志 published on October 12, 2010 9:30 AM.

http响应状态码大全,HTTP状态码反馈信息查询工具 was the previous entry in this blog.

ACCESS数据库转换为MYSQL数据库的软件 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.