使用division来选择mysql中的所有元素

时间:2013-11-28 16:32:08

标签: mysql

食物可以是简单的或Agregate ..如果它的简单它有一种类型..如果agregate它有一个简单的食物组成 我需要创建一个查询来选择所有出售所有食品类型=肉类的agregate菜肴的餐馆。我尝试了很多东西,但我无法解决问题。

    create table Restaurant(
        nameR VARCHAR(80),
        primary key (nameR));

    create table Sold(
        nameF VARCHAR(80),
        nameR VARCHAR(80),
        date VARCHAR(20);
        constraint c3 primary key(nameF,nameR,date),
        foreign key(nameF) references Dish(nameF),
        foreign key(nameR) references Restaurante(nameR),
        foreign key(date) references Data(date));

    create table Dishe(
        nameF VARCHAR(80),
        primary key (nameF),
        foreign key (nameF) references Food(nameF));

   create table Simple( 
       nameF VARCHAR(80), 
      type VARCHAR(80),
      primary key (nameF), 
       foreign key (nameF) references Food(nameF));

    create table Agregate(
       nameF VARCHAR(80),
       calorias double,
       primary key (nameF),
       foreign key (nameF) references Food(nameF));

    create table Composition(
       nameAgg VARCHAR(80),
        nameS VARCHAR(80),
        quantidade integer,
       constraint c1 primary key(nameAgg,nameS),
       foreign key(nameAgg) references Agregate(nameF),
      foreign key(nameS) references Simple(nameF));

    create table Food( 
         nameF varchar(80)
         primary key (nameF));

我认为我应该使用分组吗?告诉我你的想法

感谢

1 个答案:

答案 0 :(得分:1)

你的SQL有一些语法错误,它还引用外键“已售出”中的数据表,我不知道为什么“Dishe”表存在 - 它似乎只存储与食物相同的数据?

无论如何,这是用于创建表格的更正SQL:

create table Restaurant(
    nameR VARCHAR(80),
    primary key (nameR)
);

create table Food( 
    nameF varchar(80), 
    type varchar(80),
    primary key (nameF)
);

create table Dishe(
    nameF VARCHAR(80),
    primary key (nameF),
    foreign key (nameF) references Food(nameF)
);

create table Sold(
    nameF VARCHAR(80),
    nameR VARCHAR(80),
    date VARCHAR(20),
    constraint c3 primary key(nameF,nameR,date),
    foreign key(nameF) references Dishe(nameF),
    foreign key(nameR) references Restaurant(nameR)
);

我们可以插入一些测试数据:

INSERT INTO Restaurant (nameR) VALUES ('McDonalds');
INSERT INTO Restaurant (nameR) VALUES ('Vital Ingredient');
INSERT INTO Restaurant (nameR) VALUES ('David Food');
INSERT INTO Food (nameF, type) VALUES ('Burger', 'meat');
INSERT INTO Food (nameF, type) VALUES ('Salad', 'veg');
INSERT INTO Dishe (nameF) VALUES ('Burger');
INSERT INTO Dishe (nameF) VALUES ('Salad');
INSERT INTO Sold VALUES ('Salad', 'David Food', '2013-12-05');
INSERT INTO Sold VALUES ('Burger', 'McDonalds', '2013-12-04');
INSERT INTO Sold VALUES ('Burger', 'McDonalds', '2013-12-05');
INSERT INTO Sold VALUES ('Burger', 'Vital Ingredient', '2013-12-05');
INSERT INTO Sold VALUES ('Salad', 'Vital Ingredient', '2013-12-04');

这创造了三家餐厅,只有一家餐厅只售卖了肉类菜肴。

我能想到的两种方法可以归还售卖所有肉类菜肴的餐厅。第一个不使用分区 - 它使用GROUP_CONCAT功能将每个餐厅提供的所有类型的菜肴粘在一起,看看这是否等于“肉”:

SELECT 
    Sold.nameR
FROM
    Sold
INNER JOIN
    Food
ON
    Food.nameF = Sold.nameF
GROUP BY 
    Sold.nameR
HAVING
    GROUP_CONCAT(DISTINCT Food.type) = 'meat'
;

第二个使用分区来计算每个餐馆出售的肉类菜肴的数量,并将其除以服务的菜肴总数,并检查它是否等于1 - 即100%的菜肴是肉。由于子查询,此方法可能比上面的方法慢:

SELECT 
    Restaurant.nameR
FROM
    Restaurant
WHERE
    (
    SELECT 
        COUNT(*) 
    FROM 
        Sold 
    INNER JOIN 
        Food 
    ON 
        Food.nameF = Sold.nameF 
    WHERE 
        Food.type = 'meat' 
    AND 
        Sold.nameR = Restaurant.nameR 
    ) / (
    SELECT 
        COUNT(*) 
    FROM 
        Sold 
    INNER JOIN 
        Food 
    ON 
        Food.nameF = Sold.nameF 
    WHERE 
        Sold.nameR = Restaurant.nameR 
    ) = 1
;