The SELECT statement is used to pull information from a table. The general form of the statement is:
SELECT what_to_select FROM which_table WHERE conditions_to_satisfy;
使用WHERE指定筛选条件
官网文档的示例中,有下表pet:
+----------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+--------+---------+------+------------+------------+ | Fluffy | Harold | cat | f |1993-02-04|NULL| | Claws | Gwen | cat | m |1994-03-17|NULL| | Buffy | Harold | dog | f |1989-05-13|NULL| | Fang | Benny | dog | m |1990-08-27|NULL| | Bowser | Diane | dog | m |1979-08-31|1995-07-29| | Chirpy | Gwen | bird | f |1998-09-11|NULL| | Whistler | Gwen | bird |NULL|1997-12-09|NULL| | Slim | Benny | snake | m |1996-04-29|NULL| | Puffball | Diane | hamster | f |1999-03-30|NULL| +----------+--------+---------+------+------------+------------+
选择特定行
You can select only particular rows from your table. For example, if you want to verify the change that you made to Bowser’s birth date, select Bowser’s record like this:
SELECT*FROM pet WHERE name ='Bowser';
输出为:
+--------+-------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+-------+---------+------+------------+------------+ | Bowser | Diane | dog | m |1989-08-31|1995-07-29| +--------+-------+---------+------+------------+------------+
更复杂的示例使用了逻辑运算符:
SELECT*FROM pet WHERE (species ='cat'AND sex ='m') ->OR (species ='dog'AND sex ='f'); +-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Claws | Gwen | cat | m |1994-03-17|NULL| | Buffy | Harold | dog | f |1989-05-13|NULL| +-------+--------+---------+------+------------+-------+