MySQL的例子來學習查詢語句

來源:互聯網
上載者:User

自上學這麼多年以來,得出了從一個例子入手來學習是最快最有效,並能培養出很強的實踐能力,這是一種很好的學習方法。不訪試試。比如看一本書的時候從各章節的例子入手,找出不瞭解的以及不懂的還是新知識,

進而有針對性的學習。看看下面的例子:

 

 

<?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,所有行都被刪除。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.