=======================================================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;
=======================================================