mysql> explain select name from resource where status=1orderby name; +----+-------------+----------+------------+------+-----------------------------+-------------+---------+-------+------+----------+-------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+----------+------------+------+-----------------------------+-------------+---------+-------+------+----------+-------------+ |1| SIMPLE | resource |NULL|ref| resource_status,status_name | status_name |5| const |24|100.00|Using index | +----+-------------+----------+------------+------+-----------------------------+-------------+---------+-------+------+----------+-------------+ 1rowinset, 1 warning (0.00 sec)
2.2. union 联表
union 将两个表的数据合并,需要保证列数一致
会使用第一个 select 对应的列,第二个 select 仅留下数据
1 2 3 4 5 6 7 8
-- 将两个表的数据合并,只列出不同的值 select column1 from table1 union select column1 from table2 -- 允许有相同的值 select column1 from table1 unionall select column1 from table2
2.3. distinct 去重
1
selectdistinct name from table1;
2.4. group by 统计
1
select name, count(*) as count from table1 groupby name orderby count;
2.5. where
(1) null判断
1 2 3
select name from table1 where id isnotnull
select name from table1 where id isnull
2.6. concat 字符串拼接
将查询结果拼接其他字符串展示
1 2 3
SELECT concat('DROP TABLE IF EXISTS ', table_name, ';') FROM information_schema.tables WHERE table_schema ='mydb';