国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

從一個MySQL的例子來學習查詢語句

2024-07-24 13:04:40
字體:
來源:轉載
供稿:網友
自上學這么多年以來,得出了從一個例子入手來學習是最快最有效,并能培養出很強的實踐能力,這是一種很好的學習方法。不訪試試。比如看一本書的時候從各章節的例子入手,找出不了解的以及不懂的還是新知識, 
進而有針對性的學習。看看下面的例子: 

<?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."次訪問"; 
?> 
從上面的一個小小的計數器例子來看。已經包括了查詢、插入、修改的知識。可以實現功能。記錄每一個IP的訪問量及總的訪問次數。總量不支刷新 
一、查詢: 
SELECT被用來檢索從一個或多個表中精選的行。select_expression指出你想要檢索的列。SELECT也可以用來檢索不引用任何表的計算行 
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用新值更新現存表中行的列,SET子句指出哪個列要修改和他們應該被給定的值,WHERE子句,如果給出,指定哪個行應該被更新,否則所有行被更新。 
三、插入: 
1、 
insert into table1 values('field1',field2',...); 
INSERT ... VALUES形式的語句基于明確指定的值插入行. 
2、 
replace into tbl_name(1,2,3)values('1','2','3'); 
REPLACE功能與INSERT完全一樣,除了如果在表中的一個老記錄具有在一個唯一索引上的新記錄有相同的值,在新記錄被插入之前,老記錄被刪除。 
四、刪除: 
$a="delet from table1 where field1='$value1' and field2='$value2'"; 
DELETE從tbl_name表中刪除滿足由where_definition給出的條件的行,并且返回刪除記錄的個數。如果你執行一個沒有WHERE子句的DELETE,所有行都被刪除。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莲花县| 林周县| 天峨县| 安吉县| 抚远县| 白河县| 班玛县| 泰安市| 沿河| 花莲县| 大连市| 乌苏市| 龙井市| 宿迁市| 惠州市| 九江市| 江孜县| 曲沃县| 吕梁市| 义马市| 布尔津县| 麻江县| 阜城县| 松阳县| 罗定市| 柳江县| 江阴市| 吉安市| 伊金霍洛旗| 海原县| 太白县| 镇雄县| 眉山市| 望城县| 八宿县| 建瓯市| 泌阳县| 彭山县| 金乡县| 小金县| 湘阴县|