MySQL subquery connection

 

 

 

 

 

 

 

 

 

--MySQL subqueries connector
1, set names gbk: display form setting client as GBK (if before setting a utf-8 encoded character is displayed can not be recognized)
2, refers subqueries nested inside the query, must be round brackets used, the return value is a scalar, a data line is also started table
3, in another subquery is a select clause of the query to
4, during the writing of the data table, the default is not allowed to write Chinese characters therefore need to change the encoding format:
Show Full Columns from phplamp; status check data tables of all fields
alter table phplamp change address address varchar (modify utf8 encoding format
set names gbk; the gbk client modified to display the data fields to
5, the sub-query:
SELECT AVG (goods_price) from tdb_goods; were averaged for a column
select round (avg (goods_price),a column Averaging , two decimal places
select goods_id, goods_name, goods_price from tdb_goodsfind the price is greater than the level Id mean goods, name and price
select goods_id, goods_name, goods_price from tdb_goods where goods_price> = (select round (avg (goods_price), 2) from tdb_goods); sub-query
select goods_price from tdb_goods where goods_cate = "super this"; selection output over most of the notebook prices
select * from tdb_goods where goods_cate = "ultrabooks" \ G; column shows over most of the basic information notebook
6, some of the key sub-query the any / some / All / not in / in /
the SELECT goods_id, goods_name, goods_price from the WHERE tdb_goods goods_price> = any (select goods_price from tdb_goods where goods_cate = " super present"); the need to add any, all, some if subquery returns not a plurality of values in a column
select goods_id, goods_name, goods_price from tdb_goods where goods_price = any / some ( select goods_price from tdb_goods where goods_cate = "super present"); or greater, or less may be used any and all, but can be equal to only some / any
select goods_id, goods_name, goods_price from tdb_goods where goods_price not in (select goods_price from tdb_goods where goods_cate = " super present") \ G; not in and! = all equivalent
select goods_cate from tdb_goods group by goods_cate; group by using results of statistical classification function
insert tdb_goods_cate (cate_name) select goods_cate from tdb_goods group by goods_cate; insert-select query statement
7, a multi-function table update
multistep Update :
update the Join tdb_goods_cate tdb_goods inner goods_cate = ON SET goods_cate cate_name = cate_id; update the connection mode, multi-step update - Creative key table - records written by insert-select - table updating
step updates manner: crested select functions:
Create table tdb_goods_band (
-> Primary Key band_id unsigned smallint The AUTO_INCREMENT,
-> BRAND_NAME VARCHAR (50) Character SET utf8 COLLATE utf8_unicode_ci Not null) Character encoding may be imported to utf8
-> select brand_name from tdb_goods group by brand_name;
After the update we need to modify the name and attribute data columns together:
the ALTER the Table tdb_goods
-> Change goods_cate cate_id smallint unsigned not null,
-> Change BRAND_NAME brand_id smallint unsigned not null;
character modify the numeric type, narrow space
8, multi-table joins
connections within: two tables show only the records that meet the connection conditions
select goods_id, goods_name, cate_name from tdb_goods inner join tdb_goods_cate on tdb_goods.cate_id = tdb_goods_cate.cate_id;
left outer : the right and left of the display all records matching records
select goods_id, goods_name, cate_name from tdb_goods left join tdb_goods_cate on tdb_goods.cate_id = tdb_goods_cate.cate_id;
right outer connection: connecting the right and left table all records matching records table
select goods_id , goods_name, cate_name from tdb_goods right join tdb_goods_cate on tdb_goods.cate_id = tdb_goods_cate.cate_id;
The connection between the plurality of tables (and the two tables are similar)
SELECT goods_id, goods_name, cate_name, BRAND_NAME, goods_price from the Join tdb_goods_cate Inner tdb_goods AS AS G C = ON g.cate_id c.cate_id Inner AS B ON the Join tdb_goods_band = b.band_id g.brand_id;
. 9, a design of wireless classification level (substructure connection)
the CREATE tABLE tdb_goods_types (
the type_id the AUTO_INCREMENT SMALLINT UNSIGNED a PRIMARY KEY,
TYPE_NAME VARCHAR (20 is) Character SET UTF8 COLLATE utf8_unicode_ci Not null,
the parent_id SMALLINT UNSIGNED the NOT nULL 0 the DEFAULT
);
own connection - nested
select s.type_id, s.type_name, count (p.type_name ) from tdb_goods_types as s left join tdb_goods_types as p on s.parent_id = p.type_id; left table subtable
select p.type_id, p.type_name, count (s.type_name ) from tdb_goods_types as p left join tdb_goods_types as s on s.parent_id = p.type_id group by p.type_name order by p.type_id; right table parent table
10, multi-table deletion
(1) the recording is repeated merge operation:
SELECT goods_id, goods_name from tdb_goods Group by goods_name; to goods_name as repeating combined field
(2) multi-table delete - control remove
delete t1 from tdb_goods as t1 left join (select goods_id, goods_name from tdb_goods group by goods_name having count ( goods_name)> = 2) as t2 on t1.goods_name = t2.goods_name where t1.goods_id> t2.goods_id;

 

Guess you like

Origin www.cnblogs.com/Yanjy-OnlyOne/p/12628552.html