MySQL-SQL基础-查询2

Posted 裸奔的小鸵鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL-SQL基础-查询2相关的知识,希望对你有一定的参考价值。


mysql> create table customer(mid char(5) primary key,th date,sex char(1) default 0); Query OK, 0 rows affected (0.01 sec) mysql> desc customer; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | mid | char(5) | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | | birth | date | YES | | NULL | | | sex | char(1) | YES | | 0 | | +-------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) mysql> insert into customer values(N0001,xiaoxiao,1980-11-23,1); Query OK, 1 row affected (0.00 sec) mysql> insert into customer values(N0002,bobo,1981-12-21,0); Query OK, 1 row affected (0.00 sec) mysql> create table goods(id int auto_increment primary key,name varchar(30)); Query OK, 0 rows affected (0.01 sec) mysql> insert into goods values(1,taotao); Query OK, 1 row affected (0.01 sec) mysql> insert into goods values(2,meimei); Query OK, 1 row affected (0.00 sec) mysql> select * from goods; +----+--------+ | id | name | +----+--------+ | 1 | taotao | | 2 | meimei | +----+--------+ 2 rows in set (0.00 sec) mysql> select * from customer; +-------+----------+------------+------+ | mid | name | birth | sex | +-------+----------+------------+------+ | N0001 | xiaoxiao | 1980-11-23 | 1 | | N0002 | bobo | 1981-12-21 | 0 | +-------+----------+------------+------+ 2 rows in set (0.00 sec) mysql> insert into customer values(G0001,dudu,1975-04-18,0); Query OK, 1 row affected (0.00 sec) mysql> insert into customer values(G0002,yuyu,1985-04-20,1); Query OK, 1 row affected (0.00 sec) mysql> select * from customer; +-------+----------+------------+------+ | mid | name | birth | sex | +-------+----------+------------+------+ | G0001 | dudu | 1975-04-18 | 0 | | G0002 | yuyu | 1985-04-20 | 1 | | N0001 | xiaoxiao | 1980-11-23 | 1 | | N0002 | bobo | 1981-12-21 | 0 | +-------+----------+------------+------+ 4 rows in set (0.00 sec) mysql> select * from goods; +----+--------+ | id | name | +----+--------+ | 1 | taotao | | 2 | meimei | +----+--------+ 2 rows in set (0.00 sec) mysql> select * from customer where birth=NULL; Empty set (0.00 sec) mysql> select name,birth,sex from customer where sex=1 and birth is not null; +----------+------------+------+ | name | birth | sex | +----------+------------+------+ | yuyu | 1985-04-20 | 1 | | xiaoxiao | 1980-11-23 | 1 | +----------+------------+------+ 2 rows in set (0.00 sec) mysql> select name,birth,sex from customer where birth<=1976-01-01 or birth>=1980-01-01 and sex=1; +----------+------------+------+ | name | birth | sex | +----------+------------+------+ | dudu | 1975-04-18 | 0 | | yuyu | 1985-04-20 | 1 | | xiaoxiao | 1980-11-23 | 1 | +----------+------------+------+ 3 rows in set (0.00 sec) mysql> select name,birth,sex from customer where (birth<=1976-01-01 or birth>=1980-01-01) and sex=1; +----------+------------+------+ | name | birth | sex | +----------+------------+------+ | yuyu | 1985-04-20 | 1 | | xiaoxiao | 1980-11-23 | 1 | +----------+------------+------+ 2 rows in set (0.01 sec) mysql> select name,birth,sex from customer order by sex asc,birth desc; +----------+------------+------+ | name | birth | sex | +----------+------------+------+ | bobo | 1981-12-21 | 0 | | dudu | 1975-04-18 | 0 | | yuyu | 1985-04-20 | 1 | | xiaoxiao | 1980-11-23 | 1 | +----------+------------+------+ 4 rows in set (0.00 sec)


 





以上是关于MySQL-SQL基础-查询2的主要内容,如果未能解决你的问题,请参考以下文章

MySQL-SQL查询

mysql-sql数据拼接

mysql-sql逻辑查询顺序

mysql-SQL优化

MySQL-SQL语法基础

mysql-sql优化前要明白的知识点