從一個(gè)MySQL的例子來學(xué)習(xí)查詢語句
更新時(shí)間:2006年12月08日 00:00:00 作者:
自上學(xué)這么多年以來,得出了從一個(gè)例子入手來學(xué)習(xí)是最快最有效,并能培養(yǎng)出很強(qiáng)的實(shí)踐能力,這是一種很好的學(xué)習(xí)方法。不訪試試。比如看一本書的時(shí)候從各章節(jié)的例子入手,找出不了解的以及不懂的還是新知識,
進(jìn)而有針對性的學(xué)習(xí)。看看下面的例子:
<?php
$ip = getenv("REMOTE_ADDR");
//echo "$ip";
$conn=mysql_connect('ip','root','****');
mysql_select_db('db',$conn);
$sql="select *from counter where ip="$ip";";
$result=mysql_query($sql,$conn);
$num=mysql_num_rows($result);
$count=mysql_query("select*from counter",$conn);
$count=mysql_num_rows($count);
//echo "$num";
//echo "$count";
if($num==0)
{
$sum=1;
$count++;
$sql="insert into counter(ip,sum,countdata)values('$ip','1','$count')";
mysql_query($sql,$conn);
mysql_close($conn);
//echo "$count";
}
else
{
$my="select sum from counter where ip='$ip'";
$bbbbbb=mysql_query($my) or die(mysql_error());
$row=mysql_fetch_Array($bbbbbb);
@$sum=$row[sum];
//echo "$sum";
$sum++;
$sql="update counter set sum='$sum' where ip='$ip'";
mysql_query($sql);
mysql_close($conn);
}
$str_count=strval($count);
$str_count=Chop($str_count);
$count_len=strlen($str_count);
$image_count="";
if($count_len<6)
{for($j=0;$j<6-$count_len;$j++)
{$image_count=$image_count."<ccid_file values="0" src=0.gif />";
}
}
for($i=0;$i<$count_len;$i++)
{
$nom=substr($str_count,$i,1);
$image_count = $image_count."<ccid_file values=";
$image_count = $image_count"
$image_count = $image_count.".gif />";
}
echo"$image_count<br>";
print"這是你的第".$sum."次訪問";
?>
從上面的一個(gè)小小的計(jì)數(shù)器例子來看。已經(jīng)包括了查詢、插入、修改的知識??梢詫?shí)現(xiàn)功能。記錄每一個(gè)IP的訪問量及總的訪問次數(shù)??偭坎恢⑿?nbsp;
一、查詢:
SELECT被用來檢索從一個(gè)或多個(gè)表中精選的行。select_expression指出你想要檢索的列。SELECT也可以用來檢索不引用任何表的計(jì)算行
1、
select*from table1;
2、
select field1,field2 from table1;
3、
select count(*)from table2;
4、
select table1.filed1,table2.filed2 from table1,table2 where table1.field1='valed1';
5、
select *from table1 where field1='valed1' and fiels2='valed2' orded by field1;
6、
select*from table1 where filed1='valed1' order byfiled1 desc linit n;
7、
select *from table2 where name like '%$lin%';(模糊)
8、
sub_selects(高級)
select *from table1 where id In(select id from table2......);
9、
select ...into table...(高級)
(可用select...into outfile...;insert...selkect...替代)
10、SELECT MAX(field1) AS field1 FROM table1 列的最大值
其中:表:table 字段:field
二、修改
"update table1 set field1='value1',field2=value2'where fiels3='values3";
UPDATE用新值更新現(xiàn)存表中行的列,SET子句指出哪個(gè)列要修改和他們應(yīng)該被給定的值,WHERE子句,如果給出,指定哪個(gè)行應(yīng)該被更新,否則所有行被更新。
三、插入:
1、
insert into table1 values('field1',field2',...);
INSERT ... VALUES形式的語句基于明確指定的值插入行.
2、
replace into tbl_name(1,2,3)values('1','2','3');
REPLACE功能與INSERT完全一樣,除了如果在表中的一個(gè)老記錄具有在一個(gè)唯一索引上的新記錄有相同的值,在新記錄被插入之前,老記錄被刪除。
四、刪除:
$a="delet from table1 where field1='$value1' and field2='$value2'";
DELETE從tbl_name表中刪除滿足由where_definition給出的條件的行,并且返回刪除記錄的個(gè)數(shù)。如果你執(zhí)行一個(gè)沒有WHERE子句的DELETE,所有行都被刪除。
進(jìn)而有針對性的學(xué)習(xí)。看看下面的例子:
<?php
$ip = getenv("REMOTE_ADDR");
//echo "$ip";
$conn=mysql_connect('ip','root','****');
mysql_select_db('db',$conn);
$sql="select *from counter where ip="$ip";";
$result=mysql_query($sql,$conn);
$num=mysql_num_rows($result);
$count=mysql_query("select*from counter",$conn);
$count=mysql_num_rows($count);
//echo "$num";
//echo "$count";
if($num==0)
{
$sum=1;
$count++;
$sql="insert into counter(ip,sum,countdata)values('$ip','1','$count')";
mysql_query($sql,$conn);
mysql_close($conn);
//echo "$count";
}
else
{
$my="select sum from counter where ip='$ip'";
$bbbbbb=mysql_query($my) or die(mysql_error());
$row=mysql_fetch_Array($bbbbbb);
@$sum=$row[sum];
//echo "$sum";
$sum++;
$sql="update counter set sum='$sum' where ip='$ip'";
mysql_query($sql);
mysql_close($conn);
}
$str_count=strval($count);
$str_count=Chop($str_count);
$count_len=strlen($str_count);
$image_count="";
if($count_len<6)
{for($j=0;$j<6-$count_len;$j++)
{$image_count=$image_count."<ccid_file values="0" src=0.gif />";
}
}
for($i=0;$i<$count_len;$i++)
{
$nom=substr($str_count,$i,1);
$image_count = $image_count."<ccid_file values=";
$image_count = $image_count"
$image_count = $image_count.".gif />";
}
echo"$image_count<br>";
print"這是你的第".$sum."次訪問";
?>
從上面的一個(gè)小小的計(jì)數(shù)器例子來看。已經(jīng)包括了查詢、插入、修改的知識??梢詫?shí)現(xiàn)功能。記錄每一個(gè)IP的訪問量及總的訪問次數(shù)??偭坎恢⑿?nbsp;
一、查詢:
SELECT被用來檢索從一個(gè)或多個(gè)表中精選的行。select_expression指出你想要檢索的列。SELECT也可以用來檢索不引用任何表的計(jì)算行
1、
select*from table1;
2、
select field1,field2 from table1;
3、
select count(*)from table2;
4、
select table1.filed1,table2.filed2 from table1,table2 where table1.field1='valed1';
5、
select *from table1 where field1='valed1' and fiels2='valed2' orded by field1;
6、
select*from table1 where filed1='valed1' order byfiled1 desc linit n;
7、
select *from table2 where name like '%$lin%';(模糊)
8、
sub_selects(高級)
select *from table1 where id In(select id from table2......);
9、
select ...into table...(高級)
(可用select...into outfile...;insert...selkect...替代)
10、SELECT MAX(field1) AS field1 FROM table1 列的最大值
其中:表:table 字段:field
二、修改
"update table1 set field1='value1',field2=value2'where fiels3='values3";
UPDATE用新值更新現(xiàn)存表中行的列,SET子句指出哪個(gè)列要修改和他們應(yīng)該被給定的值,WHERE子句,如果給出,指定哪個(gè)行應(yīng)該被更新,否則所有行被更新。
三、插入:
1、
insert into table1 values('field1',field2',...);
INSERT ... VALUES形式的語句基于明確指定的值插入行.
2、
replace into tbl_name(1,2,3)values('1','2','3');
REPLACE功能與INSERT完全一樣,除了如果在表中的一個(gè)老記錄具有在一個(gè)唯一索引上的新記錄有相同的值,在新記錄被插入之前,老記錄被刪除。
四、刪除:
$a="delet from table1 where field1='$value1' and field2='$value2'";
DELETE從tbl_name表中刪除滿足由where_definition給出的條件的行,并且返回刪除記錄的個(gè)數(shù)。如果你執(zhí)行一個(gè)沒有WHERE子句的DELETE,所有行都被刪除。
相關(guān)文章
學(xué)習(xí)mysql之后的一點(diǎn)總結(jié)(基礎(chǔ))
學(xué)習(xí)mysql之后的一點(diǎn)總結(jié),比較適合新手朋友2012-05-05淺析drop user與delete from mysql.user的區(qū)別
本篇文章是對drop user與delete from mysql.user的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06MySQL4 File ‘c:\mysql\share\charsets\?.conf’ not found (Errc
File ‘c:\mysql\share\charsets\?.conf’ not found (Errcode: 22) Character set ‘#33′ is not a compiled character set and is not specified in the ‘c:\mysql\share\charsets\Index’ file2013-08-08MySQL的CASE WHEN語句的幾個(gè)使用實(shí)例
這篇文章主要介紹了MySQL的CASE WHEN語句的幾個(gè)使用實(shí)例,需要的朋友可以參考下2014-05-05MySQL參數(shù)調(diào)優(yōu)實(shí)例探究講解
本文主要講解MySQL參數(shù)調(diào)優(yōu),包括SQL調(diào)優(yōu)的參數(shù)解析,如何在生產(chǎn)環(huán)境調(diào)優(yōu),文中根據(jù)不同的數(shù)據(jù)庫參數(shù)給出調(diào)優(yōu)建議與應(yīng)用場景2022-11-11