Learn the query statement from an MySQL example

  • 2020-05-06 11:47:49
  • OfStack

Since I went to school for so many years, I have come to the conclusion that learning from an example is the fastest and most effective way to develop strong practical ability, which is a good learning method. Try not to visit. For example, when you read a book, you can start from the examples of each chapter to find out what you don't understand and what you don't understand is new knowledge.  
Then targeted learning. Consider the following example:  

< ?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" this is your number ".$sum.  
? >  
Look at the little counter example above. Knowledge of queries, inserts, and modifications has been included. You can implement functionality. Record the number of visits per IP and the total number of visits. The total amount does not refresh  
I. inquiry:  
SELECT is used to retrieve selected rows from one or more tables. select_expression indicates the column you want to retrieve. SELECT can also be used to retrieve calculated rows  
that do not reference any tables 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%'; (blur)  
8,  
sub_selects(advanced)  
select   *from   table1   where   id   In(select   id   from   table2......);  
9,  
select  ... into   table... (advanced)  
(available select... into   outfile... ; insert... selkect... Instead)  
10. SELECT   MAX(field1)   AS    1   FROM   table1   maximum  
Where: table: table   field: field  
Second, modify  
"update   table1   set   field1='value1',field2=value2'where   fiels3='values3";  
The UPDATE clause indicates which columns should be modified and the values they should be given. The WHERE clause, if given, specifies which rows should be updated, otherwise all rows will be updated.  
Insert:  
1,  
insert   into   table1   values('field1',field2',...);  
INSERT  ... Statements in the form   VALUES insert rows based on explicitly specified values.  
2,  
replace   into   tbl_name(1,2,3)values('1','2','3');  
REPLACE functions exactly the same as INSERT, except that if an old record in the table has the same value as a new record on a unique index, the old record is dropped before the new record is inserted.  
Delete:  
$a="delet   from   table1   where   field1='$value1'   and   field2='$value2'";  
DELETE deletes rows from the tbl_name table that satisfy the criteria given by where_definition, and returns the number of deleted records. If you execute DELETE without an WHERE clause, all lines are deleted.

Related articles: