MySQL重要查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL重要查询相关的知识,希望对你有一定的参考价值。

  1. //Connection
  2. $con = mysql_connect("localhost","root","password");
  3. mysql_select_db("my_db", $con);
  4.  
  5. //Creat DB
  6. mysql_query("CREATE DATABASE my_db",$con)
  7.  
  8. //Create table
  9. $sql = "CREATE TABLE Persons
  10. (
  11. id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  12. FirstName varchar(15) NOT NULL,
  13. LastName varchar(15) NOT NULL,
  14. Age int,
  15. PRIMARY KEY (id)
  16. )";
  17. mysql_query($sql,$con);
  18.  
  19. //Insert row in table
  20. mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");
  21. $insertedId=mysql_insert_id();
  22.  
  23. //Update row
  24. mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
  25.  
  26. //Delete row
  27. mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
  28.  
  29. //Select one row
  30. $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'") or die(mysql_error());
  31. $row = mysql_fetch_array($result);
  32. echo $row['LastName'];
  33.  
  34. //Select more rows
  35. $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'") or die(mysql_error());
  36. while( $row = mysql_fetch_array($result) ){
  37. echo $row['LastName'];
  38. }
  39.  
  40. //Select more rows order by Age discending from the rows from 0 to 10
  41. $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter' ORDER BY Age DESC LIMIT 0,10") or die(mysql_error());
  42. while( $row = mysql_fetch_array($result) ){
  43. echo $row['LastName'];
  44. }

以上是关于MySQL重要查询的主要内容,如果未能解决你的问题,请参考以下文章

MySQL重要查询

PHP MySQL重要查询

MySQL查询优化

MySQL查询优化

MySQL查询优化

MySql-表的概念DQL查询语句条件查询模糊查询分组函数单行处理函数连接查询笛卡尔积现象内外自连接子查询重要关键字建表增删改查