View
977
Download
0
Embed Size (px)
MySQLO comando SELECT
Escola SecundriaD. Afonso SanchesVila do Conde
ordenao
Ordenao por uma coluna
Ordenao por vrias colunas
Seleco de expresses
Ordenao por posio
Ordenao e NULL
Eliminao de repeties
Introduo:
#criar a tabelaCREATE TABLE Produto(codigo INT NULL PRIMARY KEY AUTO_INCREMENT,nome VARCHAR(15) NOT NULL,preco DECIMAL(4,2) NOT NULL,stock SMALLINT NOT NULL) TYPE=InnoDB;
#inserir dados de testeINSERT INTO Produto (nome, preco, stock) VALUES('batata', 32.30, 40),('cenoura', 12, 45),('laranja', 11.3, 100),('alho', 1, 12),('cebola', 1332.50, 5),('couve', 13.00, 100);
Tabelas que vamos utilizar como exemplo:
mysql> select * from Produto;+--------+---------+-------+-------+| codigo | nome | preco | stock |+--------+---------+-------+-------+| 1 | batata | 32.30 | 40 | | 2 | cenoura | 12.00 | 45 | | 3 | laranja | 11.30 | 100 | | 4 | alho | 1.00 | 12 | | 5 | cebola | 99.99 | 5 | | 6 | couve | 13.00 | 100 | +--------+---------+-------+-------+6 rows in set (0.00 sec)
#criar a tabelaCREATE TABLE t1(c1 INT PRIMARY KEY,c2 VARCHAR(15) NOT NULL,c3 DECIMAL(4,2) NULL,c4 INT NULL) TYPE=InnoDB;#inserir dados de testeINSERT INTO t1 VALUES(3, "joana", 3, 1),(1, "carla", 3, NULL),(2, "ana", 4.8, 7),(6, "maria", 3, NULL),(4, "catarina", 3, 2),(5, "margarida", 67.4, NULL),(7, "isabel", 3.2, 0);
mysql> select * from t1;+----+-----------+-------+------+| c1 | c2 | c3 | c4 |+----+-----------+-------+------+| 1 | carla | 3.00 | NULL | | 2 | ana | 4.80 | 7 | | 3 | joana | 3.00 | 1 | | 4 | catarina | 3.00 | 2 | | 5 | margarida | 67.40 | NULL | | 6 | maria | 3.00 | NULL | | 7 | isabel | 3.20 | 0 | +----+-----------+-------+------+7 rows in set (0.00 sec)
Quando estamos a utilizar o comando SELECT e pretendemos obter os resultados organizados de uma determinada forma, devemos recorrer clusula ORDER BY.
Ordenao
A sintaxe do comando SELECT a seguinte:
SELECT campo1, campo2, campo3, ..., campon, *FROM tabela1, tabela2, tabelak[ORDER BY campo [ASC|DESC], campo [ASC|DESC], ...]
Basta indicar na clusula ORDER BY qual o nome da coluna pela qual pretendemos ordenar os resultados.
Ordenao por uma coluna
mysql> select * from Produto order by stock;+--------+---------+-------+-------+| codigo | nome | preco | stock |+--------+---------+-------+-------+| 17 | cebola | 99.99 | 5 | | 16 | alho | 1.00 | 12 | | 13 | batata | 32.30 | 40 | | 14 | cenoura | 12.00 | 45 | | 15 | laranja | 11.30 | 100 | | 18 | couve | 13.00 | 100 | +--------+---------+-------+-------+6 rows in set (0.00 sec)
mysql> select nome from Produto order by nome asc;+---------+| nome |+---------+| alho | | batata | | cebola | | cenoura | | couve | | laranja | +---------+6 rows in set (0.00 sec)
mysql> select * from Produto order by stock;+--------+---------+-------+-------+| codigo | nome | preco | stock |+--------+---------+-------+-------+| 17 | cebola | 99.99 | 5 | | 16 | alho | 1.00 | 12 | | 13 | batata | 32.30 | 40 | | 14 | cenoura | 12.00 | 45 | | 15 | laranja | 11.30 | 100 | | 18 | couve | 13.00 | 100 | +--------+---------+-------+-------+6 rows in set (0.00 sec)
mysql> select nome from Produto order by nome desc;+---------+| nome |+---------+| laranja | | couve | | cenoura | | cebola | | batata | | alho | +---------+6 rows in set (0.00 sec)
Exemplos com a tabela t1.
Ordenao por uma coluna continuao
mysql> select * from t1 order by c3 asc;+----+-----------+-------+------+| c1 | c2 | c3 | c4 |+----+-----------+-------+------+| 1 | carla | 3.00 | NULL | | 3 | joana | 3.00 | 1 | | 4 | catarina | 3.00 | 2 | | 6 | maria | 3.00 | NULL | | 7 | isabel | 3.20 | 0 | | 2 | ana | 4.80 | 7 | | 5 | margarida | 67.40 | NULL | +----+-----------+-------+------+7 rows in set (0.00 sec)
mysql> select c2 from t1 order by c2;+-----------+| c2 |+-----------+| ana | | carla | | catarina | | isabel | | joana | | margarida | | maria | +-----------+7 rows in set (0.00 sec)
mysql> select * from t1 order by c3 desc;+----+-----------+-------+------+| c1 | c2 | c3 | c4 |+----+-----------+-------+------+| 5 | margarida | 67.40 | NULL | | 2 | ana | 4.80 | 7 | | 7 | isabel | 3.20 | 0 | | 1 | carla | 3.00 | NULL | | 3 | joana | 3.00 | 1 | | 4 | catarina | 3.00 | 2 | | 6 | maria | 3.00 | NULL | +----+-----------+-------+------+7 rows in set (0.00 sec)
mysql> select c2 from t1 order by c2 desc;+-----------+| c2 |+-----------+| maria | | margarida | | joana | | isabel | | catarina | | carla | | ana | +-----------+7 rows in set (0.00 sec)
Quando a ordenao se pretende por mais do que uma coluna, basta ir acrescentanto clusula ORDER BY os nomes das colunas pelos quais se iro fazer as vrias subordenaes.
Ordenao por vrias colunas
mysql> select stock, nome from Produto order by stock, nome;+-------+---------+| stock | nome |+-------+---------+| 5 | cebola | | 12 | alho | | 40 | batata | | 45 | cenoura | | 100 | couve | | 100 | laranja | +-------+---------+6 rows in set (0.00 sec)
mysql> select stock, nome from Produto order by stock, nome desc;+-------+---------+| stock | nome |+-------+---------+| 5 | cebola | | 12 | alho | | 40 | batata | | 45 | cenoura | | 100 | laranja | | 100 | couve | +-------+---------+6 rows in set (0.00 sec)
Exemplos com a tabela t1
Ordenao por vrias colunas continuao
mysql> select c3, c2 from t1 order by c3, c2;+-------+-----------+| c3 | c2 |+-------+-----------+| 3.00 | carla | | 3.00 | catarina | | 3.00 | joana | | 3.00 | maria | | 3.20 | isabel | | 4.80 | ana | | 67.40 | margarida | +-------+-----------+7 rows in set (0.00 sec)
mysql> select c3, c2 from t1 order by c3, c2 desc;+-------+-----------+| c3 | c2 |+-------+-----------+| 3.00 | maria | | 3.00 | joana | | 3.00 | catarina | | 3.00 | carla | | 3.20 | isabel | | 4.80 | ana | | 67.40 | margarida | +-------+-----------+7 rows in set (0.00 sec)
Nem sempre desejamos obter os resultados ordenados a partir de uma coluna existente mas de um qualquer clculo que realizamos (expresso).Nesse caso a ordenao ser feita pela coluna resultante da expresso avaliada.
Ordenao por expresses
mysql> select nome, stock - codigo from Produto;+---------+----------------+| nome | stock - codigo |+---------+----------------+| batata | 27 | | cenoura | 31 | | laranja | 85 | | alho | -4 | | cebola | -12 | | couve | 82 | +---------+----------------+6 rows in set (0.00 sec)
mysql> select nome, stock - codigo as expr1 from Produto;+---------+-------+| nome | expr1 |+---------+-------+| batata | 27 | | cenoura | 31 | | laranja | 85 | | alho | -4 | | cebola | -12 | | couve | 82 | +---------+-------+6 rows in set (0.00 sec)
mysql> select nome, stock - codigo as expr1 from Produto order by expr1;+---------+-------+| nome | expr1 |+---------+-------+| cebola | -12 | | alho | -4 | | batata | 27 | | cenoura | 31 | | couve | 82 | | laranja | 85 | +---------+-------+6 rows in set (0.00 sec)
Exemplos com a tabela t1.
Ordenao por expresses continuao
mysql> select c3, 2 * c3 as dobro from t1 order by dobro;+-------+--------+| c3 | dobro |+-------+--------+| 3.00 | 6.00 | | 3.00 | 6.00 | | 3.00 | 6.00 | | 3.00 | 6.00 | | 3.20 | 6.40 | | 4.80 | 9.60 | | 67.40 | 134.80 | +-------+--------+7 rows in set (0.00 sec)
mysql> select c1, c2 from t1 order by c1 - c3;+----+-----------+| c1 | c2 |+----+-----------+| 5 | margarida | | 2 | ana | | 1 | carla | | 3 | joana | | 4 | catarina | | 6 | maria | | 7 | isabel | +----+-----------+7 rows in set (0.00 sec)
O tratamento que dado ao valor NULL varia de sistema para sistema.Neste caso acontece o seguinte:
Ordenao e NULL
mysql> select * from t1;+----+-----------+-------+------+| c1 | c2 | c3 | c4 |+----+-----------+-------+------+| 1 | carla | 3.00 | NULL | | 2 | ana | 4.80 | 7 | | 3 | joana | 3.00 | 1 | | 4 | catarina | 3.00 | 2 | | 5 | margarida | 67.40 | NULL | | 6 | maria | 3.00 | NULL | | 7 | isabel | 3.20 | 0 | +----+-----------+-------+------+7 rows in set (0.00 sec)
mysql> select * from t1 order by c4 asc;+----+-----------+-------+------+| c1 | c2 | c3 | c4 |+----+-----------+-------+------+| 1 | carla | 3.00 | NULL | | 5 | margarida | 67.40 | NULL | | 6 | maria | 3.00 | NULL | | 7 | isabel | 3.20 | 0 | | 3 | joana | 3.00 | 1 | | 4 | catarina | 3.00 | 2 | | 2 | ana | 4.80 | 7 | +----+-----------+-------+------+7 rows in set (0.00 sec)
mysql> select * from t1 order by c4 desc;+----+-----------+-------+------+| c1 | c2 | c3 | c4 |+----+-----------+-------+------+| 2 | ana | 4.80 | 7 | | 4 | catarina | 3.00 | 2 | | 3 | joana | 3.00 | 1 | | 7 | isabel | 3.20 | 0 | | 1 | carla | 3.00 | NULL | | 5 | margarida | 67.40 | NULL | | 6 | maria | 3.00 | NULL | +----+-----------+-------+------+7 rows in set (0.00 sec)
Eliminao de repeties
mysql> select * from Postal;+--------+---------------+| codigo | localidade |+--------+---------------+| 1000 | LISBOA | | 1100 | LISBOA | | 1200 | LISBOA | | 1500 | LISBOA | | 2000 | SANTAREM | | 2300 | TOMAR | | 3000 | COIMBRA | | 4000 | PORTO | | 4100 | PORTO | | 4200 | PORTO | | 4480 | VILA DO CONDE | | 9000 | FUNCHAL | +--------+---------------+12 rows in set (0.01 sec)
mysql> select all localidade from Postal;+---------------+| localidade |+---------------+| LISBOA | | LISBOA | | LISBOA | | LISBOA | | SANTAREM | | TOMAR | | COIMBRA | | PORTO | | PORTO | | PORTO | | VILA DO CONDE | | FUNCHAL | +---------------+12 rows in set (0.00 sec)
mysql> select distinct localidade from Postal;+---------------+| localidade |+---------------+| LISBOA | | SANTAREM | | TOMAR | | COIMBRA | | PORTO | | VILA DO CONDE | | FUNCHAL | +---------------+7 rows in set (0.00 sec)
Por ve