=======================================================html
SELECT <字段名表>
FROM <表或视图名>
WHERE <查询条件>
单个字段:select * from table_name where field_name in ('xx','xxx');
sql
多个字段:select * from table_name where (field_name1,field_name2) in ((xx,'abc'),(xxx,'abcde'));
oop
单个字段-单个指定值:select * from houses where purchasing_year in ('1997');
学习
单个字段-多个指定值:select * from houses where purchasing_year in ('1997','1998','1999');
3d
多个字段-多个指定值:select id from table where (num,name) in ((num1,'name1'),(num2,'name2'))
code
select * from table_name where field_name between 'xyz' and 'abcde';
select * from houses where purchasing_year between '1996' and '1998';
select * from table_name where field_name like '%n%';
select * from houses where purchasing_year like '19%';
htm
select * from houses where purchasing_year like '19';
blog
select * from houses where purchasing_year like '%19%';
排序
select * from table_name where purchasing_year is null / is not null;
select * from houses where purchasing_year is null;
索引
select * from houses where purchasing_year is not null;
select * from table_name where field_name1='x' and field_name2='y';
select * from houses where name='甲' and purchasing_year='1997';
select * from houses where name='甲' and purchasing_year='1997' and location='天河';
select * from table_name where field_name1='x' or field_name2='y';
select * from houses where name='甲' or purchasing_year='1997';
select * from houses where name='甲' or purchasing_year='1997' or location='天河';
select * from houses where name='甲' or (purchasing_year='1997' or location='天河');
select distinct field_name from table_name;
select distinct name from houses;
select xxx 表名
以后,字段名以前,将结果以字段名为准进行排序,可设置正序、逆序。select * from table_name order by field_name + asc/desc;
select * from houses order by purchasing_year;
select * from houses order by purchasing_year asc;
select * from houses order by purchasing_year desc;
select * from tableName limit i,n
select * from houses limit 2;
select * from houses limit 2,3;
========================================================
select a.name, a.house_location,a.purchasing_year, b.age from house a /*设置返回结果包含的字段*/
inner join people b /*与表b进行内链接*/
on a.name = b.name; /*当知足这个条件时返回结果*/
select a.name,a.house_location,a.purchasing_year,b.age from house a left join people b on a.name=b.name;
select a.name,a.house_location,a.purchasing_year,b.age from house a right join people b on a.name=b.name;
SELECT A.字段1, B.字段2, C.字段3, D.字段4,
FROM 表A
LEFT JOIN 表B
LEFT JOIN 表C
RIGHT JOIN 表D
ON <查询条件>;
select a.name,a.house_location,a.purchasing_year,b.age,c.price
from houses a
inner join people b /*内链接*/
right join housingprice c /*右链接*/
on a.name=b.name
and a.house_location=c.house_location;
=======================================================
select * from houses where name in (select name from poople where age < 30); /*子句结果的name为甲*/
select * from houses where name in (select name from poople where age > 30); /*子句结果为空*/
select house_location from houses group by house_location;
select house_location, count(*) as total from houses group by house_location;
select sum(age) from people;
union
:默认同加distinct的结果,即去重
union distinct
:将结果去重后返回
union all
:将全部结果返回
select name from houses union distinct select name from people;
select name from houses union all select name from people;
select name from houses union select name from people;
=======================================================