SQL语句之间是可以进行连接操作的,在一些复杂的数据操作中必须用到连接操作。简单的说就是一个SQL语句的结果可以作为相连接的SQL操作的一部分。SQL结构化查询语句,子查询是指的所有的SQL操作,并非单一的SELECT语句。
1.由比较运算符产生:
SELECT user_name,user_id,user_phone FROM users WHERE user_age >=(SELECT ROUND(AVG(age),0) FROM users);
可以使用ANY,SOME,ALL对查询进行。
SELECT user_name,user_id,user_phone FROM users WHERE user_age >= ANY (SELECT ROUND(AVG(age),0) FROM users);
2.由[NOT] IN/EXIS产生
SELECT user_name,user_id,user_phone FROM users WHERE user_age >= NOT IN (SELECT ROUND(AVG(age),0) FROM users);
3.由INSERT......SELECT产生
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
UPDATE tdb_goods INNER JOIN tdb_goods_brands ON brand_name = brand_name SET brand_name = brand_id;