Learning query statement from a MysqL example _ MySQL

Source: Internet
Author: User
Since I have been in school for so many years, I have come to the conclusion that learning from an example is the fastest and most effective, and can cultivate a strong practical ability. this is a good way to learn. Try not to access. For example, when reading a book, you can start with the examples of each chapter to find out what you don't know and what you don't understand is new knowledge, and then learn it in a targeted manner. Let's look at the example below :? Php $ ipgetenv (R MySQL Query

Since I have been in school for so many years, I have come to the conclusion that learning from an example is the fastest and most effective, and can cultivate a strong practical ability.

Good learning methods. Try not to access. For example, when reading a book, you can start with the examples of each chapter to find out whether or not you do not understand new knowledge,

Further targeted learning. Take a look at the following example:

$ 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 ($ bbbbbbbb );
@ $ 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
";
Print "this is your". $ sum. "secondary access ";
?>


From a small counter example above. Knowledge about Query, insert, and modification is included. Functions can be implemented. Record

Traffic volume and the total number of visits. Total volume not refresh

I. query:
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 the computing rows that do 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 linit n;
7. select * from table2 where name like '% $ lin %'; (fuzzy)
8. sub_selects (advanced)
Select * from table1 where id In (select id from table2 ......);
9. select... into table... (advanced)
(This can be replaced by select... into outfile...; insert... selkect)
10. select max (field1) AS field1 FROM the maximum value of the table1 column
Where: table \ field: field

II. modification:
"Update table1 set field1 = 'value1', field2 = value2' where fiels3 = 'values3 ";
UPDATE uses a new value to UPDATE the columns in the row of an existing table. The SET clause specifies the column to be modified and the value they should be given. if

Specifies the row to be updated. otherwise, all rows are updated.

3. insert:
1. insert into table1 values ('field1', field2 ',...);
INSERT... VALUES statements INSERT rows based on specified VALUES.
2. replace into tbl_name (1, 2, 3) values ('1', '2', '3 ');
The REPLACE function is exactly the same as the INSERT function, except that an old record in the table has the same value for a new record on a unique index.

Before the new record is inserted, the old record is deleted.

IV. delete:
$ A = "delet from table1 where field1 = '$ value1' and field2 = '$ value2 '";
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

A row has a DELETE statement without a 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.