MySQL 记录操作&连接

记录

插入记录

INSERT [ INTO ] tb_name [ (col_name,...)] {VALUES | VALUE}

({expr | DEFAULT} ,...) , (...) , ...;
INSERT [INTO] tb_name SET col_name = { expr | DEFAULT } , ...
INSERT [INTO] tb_name [(col_name,...) SELECT ...

eg.:

INSERT test VALUES('max',13,1000);
INSERT test(username) SELECT username FROM users WHERE age >=30;

查找记录

查询表达式(select_expr)

SELECT select_expr [,select_expr ...]

[

FROM table_references

[WHERE where_condition]

[GROUP BY {col_name | position} [ ASC | DESC],...]

[HAVING where_condition]

[ORDER BY {col_name | expr | position} [ASC | DESC],...]

[LIMIT {[offset,] row_count | row_count OFFSET offset }]

]
  • 星号(*)表示所有
  • 查询表达式可用 [AS] alias_name 赋予别名,可用于 GROUP BY ,ORDRE BY ,HAVING
  • GROUP BY 查询结果分组ASC升序,DESC降序
  • HAVING 分组设置条件
  • ORDER BY 对查询结果进行排序
  • LIMIT 限制返回查询结果的数量

eg.:#没有WHERE语句,则默认选择所有

SELECT * FROM test;



SELECT sex FROM users GROUP BY sex;



SELECT sex,age FROM users GROUP BY sex HAVING age > 18;



SELECT * FROM users ORDER BY id DESC;



SELECT * FROM users LIMIT 2;



SELECT * FROM users LIMIT 2,2; #从0开始,第一个2是从第三开始查询



SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING count(good_name) >=2 ;

子查询(Sub Query):

  • 另一个查询语句中的SELECT子句
  • 必须在圆括号里面
  • 外查询可以是SELECT,INSERT,UPDATE,SET,DO
  • 返回标量、一行、一列、子查询
  • 比较运算符 != >= <= = < > <=> <>
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

\G;#记录显示

SET NAMES gbk;

更新记录

单表更新

UPDATE [LOW_PRIORITY][IGNOGE] table_reference SET

col_name1 = {expr1 | DEFAULT} [, col_name2 = { expr2 | DEFAULT }]...

[WHERE where_condition]

多表更新

UPDATE table_references

SET col_name1 = {expr1|DEFAULT}

[,col_name2 = {expr2|DEFAULT}] ...

[WHERE where_condition]

//更新多表

UPDATE tdb_goods INNER JOIN tdb_goods_cates ON good_cate = cate_name

SET goods_cate = cate_id;

创建表时,将查询结果写入表

CREATE TABLE [IF NOT EXISTS] tbl_name

[(create_definition,...)]

select_statement

//创建时,填入查询结果

CREATE TABLE tdb_goods_brands

(

brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,

brand_name VARCHAR(40) NOT NULL

)

SELECT brand_name FROM tdb_goods GROUP BY brand_name;

删除记录

DELETE FROM tb_name [WHERE where_condition]

多表删除

DELETE tbl_name[.*][,tbl_name[.*]]...

FROM table_references

[WHERE where_condition]

//删除重复

DELETE t1 FROM tdb_goods AS t1 LEFT JOIN (
SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name 
HAVING count(good_name) >=2 ) AS t2 
ON t1.goods_name = t2.goods_name 
WHERE t1.goods_id > t2.goods_id;

 连接

table_reference

{[INNER | CROSS] JOIN | {LEFT |RIGHT} [OUTER] JOIN}

table_reference

ON conditional_expr

//多表连接

SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods

AS g

INNER JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_id

INNER JOIN tdb_goods_brands AS b ON g.brand_id = b.brands_id\G;

连接类型

MySQL中,JOIN,CROSS JOIN 和INNER JOIN是等价的

  • INNER JOIN ,内连接:显示左、右表符合连接条件的记录
  • LEFT [OUTER] JOIN ,左外连接:显示左表全部、右表符合条件的记录
  • RIGHT [OUTER] JOIN ,右外连接:显示右表全部、左表符合条件的记录

连接条件

  • 使用ON设定连接条件
  • 使用WHERE进行结果的过滤

//查询所有商品的详细信息

内连接

SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g

INNER JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_id

INNER JOIN tdb_goods_brands AS b ON g.brand_id = b.brand_id\G;

左外连接

SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g

LEFT JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_id

LEFT JOIN tdb_goods_brands AS b ON g.brand_id = b.brand_id\G;

右外连接

SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g

RIGHT JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_id

RIGHT JOIN tdb_goods_brands AS b ON g.brand_id = b.brand_id\G;

无限级分类表

 CREATE TABLE tdb_goods_types(
     type_id   SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
     type_name VARCHAR(20) NOT NULL,
     parent_id SMALLINT UNSIGNED NOT NULL DEFAULT 0
  ); 

自身连接:对自己进行连接

//查询目前父类名字、子类id名字(一级查询)

SELECT s.type_id,s.type_name,p.type_name FROM tdb.goods_types 
AS s LEFT JOIN tdb_goods_types AS p
ON s.parent_id = p.type_id;

数据表参照

table_reference

tbl_name [[AS] alias] | table_subquery [AS] alias

数据表可以使用赋予别名

  • tbl_name AS alias_name
  • tbl_name alias_name

table_subquery可以作为子查询使用在FROM子句中,这样的子查询必须为其赋予别名

猜你喜欢

转载自blog.csdn.net/weixin_38500325/article/details/81478676