【发布时间】:2012-01-03 01:23:25
【问题描述】:
我目前正在为饮料食谱设计一个数据库 (MySQL),并希望用户能够输入他们拥有的成分,我会给他们所有包含仅这些成分的食谱。我需要的是一个查询,我可以从表 drinks 中选择所有饮料,其中唯一的成分是 like(这样做的原因是有人可能会输入伏特加或 smirnoff 伏特加,但我会处理它们是一样的)表中的成分user_ingredients
以下是此查询中将涉及的三个表:
饮料
CREATE TABLE `drinks` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`drink` varchar(64) NOT NULL DEFAULT '',
`glass` int(11) unsigned NOT NULL,
`instructions` text,
PRIMARY KEY (`id`),
UNIQUE KEY `un_drink` (`drink`),
KEY `in_id` (`id`),
KEY `fk_glass` (`glass`),
CONSTRAINT `fk_glass` FOREIGN KEY (`glass`) REFERENCES `glasses` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=11946 DEFAULT CHARSET=utf8;
recipe_ingredients
CREATE TABLE `drink_ingredients` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`drink` int(11) unsigned NOT NULL,
`ingredient` int(11) unsigned NOT NULL,
`amount` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `in_drink` (`drink`),
KEY `in_ingredient` (`ingredient`),
KEY `fk_amount` (`amount`),
CONSTRAINT `fk_amount` FOREIGN KEY (`amount`) REFERENCES `amounts` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_drink` FOREIGN KEY (`drink`) REFERENCES `drinks` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_ingredient` FOREIGN KEY (`ingredient`) REFERENCES `ingredients` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=46026 DEFAULT CHARSET=utf8;
user_ingredients
CREATE TABLE `user_ingredients` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user` int(11) unsigned DEFAULT NULL,
`ingredient` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_user_ingredient` (`ingredient`),
CONSTRAINT `fk_user_ingredient` FOREIGN KEY (`ingredient`) REFERENCES `ingredients` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
谢谢你,我在这个问题上纠结了一段时间。 -斯蒂芬
【问题讨论】: