Learn about query statements from a MySQL example

Source: Internet
Author: User
mysql| statement since school so many years, has come to learn from an example is the fastest and most effective, and can cultivate a strong practical ability, which is a very

A good way to learn. Try not to visit. For example, when you look at a book, start with the examples from each chapter, and find out what you don't know and what you don't know or new knowledge.

And then targeted learning. Take a look at 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. " ';
}
}
for ($i =0; $i < $count _len; $i + +)
{
$nom =substr ($str _count, $i, 1);
$image _count = $image _count. " $image _count = $image _count. $nom;
$image _count = $image _count. ". GIF ' > ';
}
echo "$image _count<br>";
Print "This is your first". $sum. " Visits ";
?>


From the above a small counter example to see. The knowledge of queries, inserts, and modifications has been included. can implement functionality. Record each IP's

The amount of traffic and the total number of visits. Total Not flush

First, 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 a calculated row that does not reference any table
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 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 ... High
(Available Select...into outfile ...; Insert...selkect ... Alternative
10, SELECT Max (field1) as field1 the maximum value from table1 column
Where: tables: Table \ fields: Field

Second, modify:
"Update table1 set field1= ' value1 ', field2=value2 ' where fiels3= ' values3 '";
Update updates the columns of rows in an existing table with the new value, the SET clause indicates which column to modify and what values they should be given, where clause, if

Given, specifies which row should be updated, otherwise all rows are updated.

Third, insert:
1, insert into table1 values (' field1 ', Field2 ',...);
INSERT ... Statements in the form of values insert rows based on a value that is explicitly specified.
2, replace into tbl_name (1,2,3) VALUES (' 1 ', ' 2 ', ' 3 ');
The Replace function is 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 deleted before the new record is inserted.

Four, delete:
$a = "delet from table1 where field1= ' $value 1 ' and field2= ' $value 2 '";
Delete Deletes rows from the Tbl_name table that meet the conditions given by where_definition, and returns the number of deleted records. If you hold

Row a delete with no WHERE clause, all rows are deleted.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.