Utilizando o MySQL
Primeiro devemos acessar o nosso banco de dados instalado:
mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.22-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Inicialmente iremos criar um usuario para nossa utilização
mysql> CREATE USER 'ecommerce_user'@'localhost' IDENTIFIED BY 'S3nh4S3gur4!';
Recomendo utilizar outra senha :D
Agora adicionaremos permissões para esse usuário
mysql> GRANT ALL PRIVILEGES ON * . * TO 'ecommerce_user'@'localhost';
Crie um banco de dados
mysql> create database ecommerce;
Agora devemos informar ao MySQL qual bando de dados iremos utilizar
mysql> use ecommerce;
Crie a tabela de categorias
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`description` text NOT NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=19;
Adicione alguns dados
INSERT INTO `categories` (`id`, `name`, `description`, `created`, `modified`) VALUES
(1, 'Fashion', 'Category for anything related to fashion.', '2014-06-01 00:35:07', '2014-05-30 17:34:33'),
(2, 'Electronics', 'Gadgets, drones and more.', '2014-06-01 00:35:07', '2014-05-30 17:34:33'),
(3, 'Motors', 'Motor sports and more', '2014-06-01 00:35:07', '2014-05-30 17:34:54'),
(5, 'Movies', 'Movie products.', '0000-00-00 00:00:00', '2016-01-08 13:27:26'),
(6, 'Books', 'Kindle books, audio books and more.', '0000-00-00 00:00:00', '2016-01-08 13:27:47'),
(13, 'Sports', 'Drop into new winter gear.', '2016-01-09 02:24:24', '2016-01-09 01:24:24');
Se desejar pode selecionar as categorias com o comando:
mysql> SELECT * FROM categories;
+----+-------------+-------------------------------------------+---------------------+---------------------+
| id | name | description | created | modified |
+----+-------------+-------------------------------------------+---------------------+---------------------+
| 1 | Fashion | Category for anything related to fashion. | 2014-06-01 00:35:07 | 2014-05-30 17:34:33 |
| 2 | Electronics | Gadgets, drones and more. | 2014-06-01 00:35:07 | 2014-05-30 17:34:33 |
| 3 | Motors | Motor sports and more | 2014-06-01 00:35:07 | 2014-05-30 17:34:54 |
| 5 | Movies | Movie products. | 0000-00-00 00:00:00 | 2016-01-08 13:27:26 |
| 6 | Books | Kindle books, audio books and more. | 0000-00-00 00:00:00 | 2016-01-08 13:27:47 |
| 13 | Sports | Drop into new winter gear. | 2016-01-09 02:24:24 | 2016-01-09 01:24:24 |
+----+-------------+-------------------------------------------+---------------------+---------------------+
6 rows in set (0.00 sec)
Crie a tabela de produtos
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`description` text NOT NULL,
`price` decimal(10,0) NOT NULL,
`category_id` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=65;
Adicione alguns produtos
INSERT INTO `products` (`id`, `name`, `description`, `price`, `category_id`, `created`, `modified`) VALUES
(1, 'LG P880 4X HD', 'My first awesome phone!', '336', 3, '2014-06-01 01:12:26', '2014-05-31 17:12:26'),
(2, 'Google Nexus 4', 'The most awesome phone of 2013!', '299', 2, '2014-06-01 01:12:26', '2014-05-31 17:12:26'),
(3, 'Samsung Galaxy S4', 'How about no?', '600', 3, '2014-06-01 01:12:26', '2014-05-31 17:12:26'),
(6, 'Bench Shirt', 'The best shirt!', '29', 1, '2014-06-01 01:12:26', '2014-05-31 02:12:21'),
(7, 'Lenovo Laptop', 'My business partner.', '399', 2, '2014-06-01 01:13:45', '2014-05-31 02:13:39'),
(8, 'Samsung Galaxy Tab 10.1', 'Good tablet.', '259', 2, '2014-06-01 01:14:13', '2014-05-31 02:14:08'),
(9, 'Spalding Watch', 'My sports watch.', '199', 1, '2014-06-01 01:18:36', '2014-05-31 02:18:31'),
(10, 'Sony Smart Watch', 'The coolest smart watch!', '300', 2, '2014-06-06 17:10:01', '2014-06-05 18:09:51'),
(11, 'Huawei Y300', 'For testing purposes.', '100', 2, '2014-06-06 17:11:04', '2014-06-05 18:10:54'),
(12, 'Abercrombie Lake Arnold Shirt', 'Perfect as gift!', '60', 1, '2014-06-06 17:12:21', '2014-06-05 18:12:11'),
(13, 'Abercrombie Allen Brook Shirt', 'Cool red shirt!', '70', 1, '2014-06-06 17:12:59', '2014-06-05 18:12:49'),
(26, 'Another product', 'Awesome product!', '555', 2, '2014-11-22 19:07:34', '2014-11-21 20:07:34'),
(28, 'Wallet', 'You can absolutely use this one!', '799', 6, '2014-12-04 21:12:03', '2014-12-03 22:12:03'),
(31, 'Amanda Waller Shirt', 'New awesome shirt!', '333', 1, '2014-12-13 00:52:54', '2014-12-12 01:52:54'),
(42, 'Nike Shoes for Men', 'Nike Shoes', '12999', 3, '2015-12-12 06:47:08', '2015-12-12 05:47:08'),
(48, 'Bristol Shoes', 'Awesome shoes.', '999', 5, '2016-01-08 06:36:37', '2016-01-08 05:36:37'),
(60, 'Rolex Watch', 'Luxury watch.', '25000', 1, '2016-01-11 15:46:02', '2016-01-11 14:46:02');
Se desejar pode selecionar os dados de produtos com o comando
mysql> SELECT * FROM products;
+----+-------------------------------+----------------------------------+-------+-------------+---------------------+---------------------+
| id | name | description | price | category_id | created | modified |
+----+-------------------------------+----------------------------------+-------+-------------+---------------------+---------------------+
| 1 | LG P880 4X HD | My first awesome phone! | 336 | 3 | 2014-06-01 01:12:26 | 2014-05-31 17:12:26 |
| 2 | Google Nexus 4 | The most awesome phone of 2013! | 299 | 2 | 2014-06-01 01:12:26 | 2014-05-31 17:12:26 |
| 3 | Samsung Galaxy S4 | How about no? | 600 | 3 | 2014-06-01 01:12:26 | 2014-05-31 17:12:26 |
| 6 | Bench Shirt | The best shirt! | 29 | 1 | 2014-06-01 01:12:26 | 2014-05-31 02:12:21 |
| 7 | Lenovo Laptop | My business partner. | 399 | 2 | 2014-06-01 01:13:45 | 2014-05-31 02:13:39 |
| 8 | Samsung Galaxy Tab 10.1 | Good tablet. | 259 | 2 | 2014-06-01 01:14:13 | 2014-05-31 02:14:08 |
| 9 | Spalding Watch | My sports watch. | 199 | 1 | 2014-06-01 01:18:36 | 2014-05-31 02:18:31 |
| 10 | Sony Smart Watch | The coolest smart watch! | 300 | 2 | 2014-06-06 17:10:01 | 2014-06-05 18:09:51 |
| 11 | Huawei Y300 | For testing purposes. | 100 | 2 | 2014-06-06 17:11:04 | 2014-06-05 18:10:54 |
| 12 | Abercrombie Lake Arnold Shirt | Perfect as gift! | 60 | 1 | 2014-06-06 17:12:21 | 2014-06-05 18:12:11 |
| 13 | Abercrombie Allen Brook Shirt | Cool red shirt! | 70 | 1 | 2014-06-06 17:12:59 | 2014-06-05 18:12:49 |
| 26 | Another product | Awesome product! | 555 | 2 | 2014-11-22 19:07:34 | 2014-11-21 20:07:34 |
| 28 | Wallet | You can absolutely use this one! | 799 | 6 | 2014-12-04 21:12:03 | 2014-12-03 22:12:03 |
| 31 | Amanda Waller Shirt | New awesome shirt! | 333 | 1 | 2014-12-13 00:52:54 | 2014-12-12 01:52:54 |
| 42 | Nike Shoes for Men | Nike Shoes | 12999 | 3 | 2015-12-12 06:47:08 | 2015-12-12 05:47:08 |
| 48 | Bristol Shoes | Awesome shoes. | 999 | 5 | 2016-01-08 06:36:37 | 2016-01-08 05:36:37 |
| 60 | Rolex Watch | Luxury watch. | 25000 | 1 | 2016-01-11 15:46:02 | 2016-01-11 14:46:02 |
+----+-------------------------------+----------------------------------+-------+-------------+---------------------+---------------------+
17 rows in set (0.00 sec)
Inner Join
Buscar o nome das categorias
SELECT p.name AS 'Product Name',
c.name AS 'Category Name'
FROM products p
INNER JOIN categories c ON p.category_id = c.id;
Group By
Buscar o nome das categorias
SELECT c.name AS 'Category Name',
COUNT(c.name) AS 'Quantity'
FROM products p
INNER JOIN categories c ON p.category_id = c.id
GROUP BY c.name;