I have 3 related tables and I would like to show with a query sql a table with the "titulo" of the table "recetas" with the "categoria" of the table "categorias" that is selected, a recipe can have more than one category for that I have created the table "recetas-categorias", I have tried with subqueries but I do not get it, I am a beginner in sql.
SELECT r.titulo
FROM recetas r
WHERE r.idreceta=(SELECT rc.idreceta
FROM recetas-categorias
WHERE rc.idcategoria=(SELECT id
FROM categorias c
WHERE c.categoria="Internacional"));
relationship of the three tables
Attached code of the structure and data
Recetas table
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
DROP TABLE IF EXISTS `recetas`;
CREATE TABLE IF NOT EXISTS `recetas` (
`idreceta` int(11) NOT NULL AUTO_INCREMENT,
`titulo` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
`contenido` mediumtext COLLATE utf8_spanish2_ci NOT NULL,
`fecha` date NOT NULL,
`idusuario` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
`imagenprincipal` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
`valoracion` float DEFAULT NULL,
PRIMARY KEY (`idreceta`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci;
INSERT INTO `recetas` (`idreceta`, `titulo`, `contenido`, `fecha`, `idusuario`, `imagenprincipal`, `valoracion`) VALUES
(1, 'Paella mixta', '<ul class=\"list-group\">\r\n <li class=\"list-group-item\">Cras justo odio</li>\r\n <li class=\"list-group-item\">Dapibus ac facilisis in</li>\r\n <li class=\"list-group-item\">Morbi leo risus</li>\r\n <li class=\"list-group-item\">Porta ac consectetur ac</li>\r\n <li class=\"list-group-item\">Vestibulum at eros</li>\r\n</ul>', '2019-01-15', '1', 'paella.jpg', NULL),
(2, 'Boquerones', '<ul class=\"list-group\">\r\n <li class=\"list-group-item\">Cras justo odio</li>\r\n <li class=\"list-group-item\">Dapibus ac facilisis in</li>\r\n <li class=\"list-group-item\">Morbi leo risus</li>\r\n <li class=\"list-group-item\">Porta ac consectetur ac</li>\r\n <li class=\"list-group-item\">Vestibulum at eros</li>\r\n</ul>', '2019-01-24', '1', 'boquerones.jpg', NULL),
(3, 'Salchichas', '<ul class=\"list-group\">\r\n <li class=\"list-group-item\">Cras justo odio</li>\r\n <li class=\"list-group-item\">Dapibus ac facilisis in</li>\r\n <li class=\"list-group-item\">Morbi leo risus</li>\r\n <li class=\"list-group-item\">Porta ac consectetur ac</li>\r\n <li class=\"list-group-item\">Vestibulum at eros</li>\r\n</ul>', '2019-01-24', '1', 'salchi.jpg', NULL);
COMMIT;
recetas-categorias table
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
DROP TABLE IF EXISTS `recetas-categorias`;
CREATE TABLE IF NOT EXISTS `recetas-categorias` (
`idreceta` int(11) NOT NULL,
`idcategoria` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci;
INSERT INTO `recetas-categorias` (`idreceta`, `idcategoria`) VALUES
(1, 6),
(1, 14),
(3, 9),
(3, 14),
(2, 10);
COMMIT;
categorias table
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
DROP TABLE IF EXISTS `categorias`;
CREATE TABLE IF NOT EXISTS `categorias` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`categoria` varchar(255) COLLATE utf8_spanish2_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci;
INSERT INTO `categorias` (`id`, `categoria`) VALUES
(1, 'Sopas y cremas'),
(2, 'Salsas'),
(3, 'Potajes'),
(4, 'Ensaladas'),
(5, 'Pastas'),
(6, 'Arroces'),
(7, 'Legumbres'),
(8, 'Huevos'),
(9, 'Carnes'),
(10, 'Pescados'),
(11, 'Mariscos'),
(12, 'Cocteles y refrescos'),
(13, 'Postres y repostería'),
(14, 'Internacional'),
(15, 'Robot de cocina');
COMMIT;
答案 0 :(得分:0)
您需要了解“ sql联接”。这是您如何连接这三个表的方法:
select r.titulo
from recetas r
join recetas-categorias rc
on rc.idreceta = r.idreceta
join categorias c
on c.id = rc.idcategoria
where c.categoria = 'Internacional'
答案 1 :(得分:0)
我认为,从了解您的问题开始,您可能希望将其引入MySQL表联接。
这是一个广泛的类别,在这里无法回答,因此最好将您引导到http://www.mysqltutorial.org/mysql-join/之类的地方,以了解它们的用途以及如何使用它们。
根据您的查询,进行以下修改:
SELECT r.titulo
FROM `categorias` c
INNER JOIN `recetas-categorias` rc ON rc.idcategoria= c.id
INNER JOIN `recetas` r ON r.idreceta = rc.idreceta
WHERE c.categoria="Internacional"
返回
+-------------+
| paella |
|-------------|
| salchichas |
+-------------+
...这似乎是您要实现的目标?
答案 2 :(得分:0)
对于此操作,请使用join。
SELECT r.titulo
FROM recetas r
JOIN recetas-categorias rc
ON r.idreceta = rc.idreceta
JOIN categorias c
ON rc.idcategoria = c.id
AND c.categoria="Internacional"
或
SELECT r.titulo
FROM recetas r,
recetas-categorias rc,
categorias c
WHERE r.idreceta = rc.idreceta
AND rc.idcategoria = c.id
AND c.categoria="Internacional"