MySQL學習筆記-子查詢和連接 使客戶端進入gbk編碼方式顯示: mysql> SET NAMES gbk; 1.子查詢 子查詢的定義: 子查詢(Subquery)是指出現在其他SQL語句內的SELECT子句。 例如: SELECT * FROM t1 WHERE col1 = (SELECT c ...
mysql> SET NAMES gbk;
SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2);
mysql> SELECT ROUND(avg(goods_price),2) AS avg_price FROM tdb_goods; +-----------+ | avg_price | +-----------+ | 5391.30 | +-----------+
mysql> SELECT * FROM tdb_goods WHERE goods_price > 5391.30;
mysql> SELECT * FROM tdb_goods WHERE goods_price > (SELECT ROUND(AVG(goods_price),2) FROM tdb_goods);
mysql> SELECT * FROM tdb_goods WHERE goods_cate = '超級本' ORDER BY goods_price ASC;
mysql> SELECT * FROM tdb_goods WHERE goods_price > ANY(SELECT goods_price FROM tdb_goods WHERE goods_cate = '超級本') ORDER BY goods_price DESC;
mysql> SELECT * FROM tdb_goods WHERE goods_price > ALL(SELECT goods_price FROM tdb_goods WHERE goods_cate = '超級本') ORDER BY goods_price DESC;
mysql> SELECT * FROM tdb_goods WHERE goods_cate NOT IN(SELECT goods_cate FROM tdb_goods WHERE goods_cate = '超級本');
mysql> INSERT tdb_goods_cates(cate_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate; mysql> SELECT * FROM tdb_goods_cates; +---------+---------------------+ | cate_id | cate_name | +---------+---------------------+ | 1 | 台式機 | | 2 | 平板電腦 | | 3 | 伺服器/工作站 | | 4 | 游戲本 | | 5 | 筆記本 | | 6 | 筆記本配件 | | 7 | 超級本 | +---------+---------------------+
UPDATE table_references SET col_name1={expr1|DEFAULT} [,col_name2={expr2|DEFAULT}]... [WHERE where_condition]
table_reference {[INNER | CROSS] JOIN |{LEFT|RIGHT} [OUTER] JOIN} table_reference ON conditional_expr
mysql> UPDATE tdb_goods INNER JOIN tdb_goods_cates ON goods_cate = cate_name SET goods_cate = cate_id;
CREATE TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] select_statement
mysql> CREATE TABLE tdb_goods_brand( -> brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> brand_name VARCHAR(40) NOT NULL -> ) -> SELECT brand_name FROM tdb_goods GROUP BY brand_name;
mysql> UPDATE tdb_goods INNER JOIN tdb_goods_brand ON brand_name = brand_name SET brand_name = brand_id; ERROR 1052 (23000): Column 'brand_name' in field list is ambiguous
mysql> UPDATE tdb_goods AS a INNER JOIN tdb_goods_brand AS b ON a.brand_name = b.brand_name SET a.brand_name = b.brand_id;
mysql> DESC tdb_goods; +-------------+------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------------+------+-----+---------+----------------+ | goods_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | goods_name | varchar(150) | NO | | NULL | | | goods_cate | varchar(40) | NO | | NULL | | | brand_name | varchar(40) | NO | | NULL | | | goods_price | decimal(15,3) unsigned | NO | | 0.000 | | | is_show | tinyint(1) | NO | | 1 | | | is_saleoff | tinyint(1) | NO | | 0 | | +-------------+------------------------+------+-----+---------+----------------+
mysql> ALTER TABLE tdb_goods; -> CHANGE goods_cate cate_id SMALLINT UNSIGNED NOT NULL, -> CHANGE brand_name brand_id SMALLINT UNSIGNED NOT NULL; mysql> DESC tdb_goods; +-------------+------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------------+------+-----+---------+----------------+ | goods_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | goods_name | varchar(150) | NO | | NULL | | | cate_id | smallint(5) unsigned | NO | | NULL | | | brand_id | smallint(5) unsigned | NO | | NULL | | | goods_price | decimal(15,3) unsigned | NO | | 0.000 | | | is_show | tinyint(1) | NO | | 1 | | | is_saleoff | tinyint(1) | NO | | 0 | | +-------------+------------------------+------+-----+---------+----------------+
table_reference {[INNER|CROSS] JOIN|{LEFT|RIGHT}[OUTER]JOIN} table_reference ON conditional_expr
INSERT tdb_goods_cates(cate_name) VALUES('路由器'),('交換機'),('網卡'); INSERT tdb_goods_brands(brand_name) VALUES('海爾'),('清華同方'),('神舟');
INSERT tdb_goods(goods_name,cate_id,brand_id,goods_price) VALUES(' LaserJet Pro P1606dn 黑白激光印表機','12','4','1849');
mysql> SELECT goods_id,goods_name,cate_name,goods_price FROM tdb_goods -> INNER JOIN tdb_goods_cates ON tdb_goods.cate_id = tdb_goods_cates.cate_id;
mysql> SELECT goods_id,cate_name,goods_price FROM tdb_goods LEFT JOIN tdb_goods_cates ON tdb_goods.cate_id = tdb_goods_cates.cate_id;
mysql> SELECT goods_id,goods_name,cate_name,goods_price FROM tdb_goods RIGHT JOIN tdb_goods_cates ON tdb_goods.cate_id = tdb_goods_cates.cate_id;
mysql> SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods -> INNER JOIN tdb_goods_cates ON tdb_goods.cate_id = tdb_goods_cates.cate_id -> INNER JOIN tdb_goods_brand ON tdb_goods.brand_id = tdb_goods_brand.brand_id;
mysql> 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; +---------+-----------------+-----------------+ | type_id | type_name | type_name | +---------+-----------------+-----------------+ | 1 | 家用電器 | NULL | | 2 | 電腦、辦公 | NULL | | 3 | 大家電 | 家用電器 | | 4 | 生活電器 | 家用電器 | | 5 | 平板電視