SQL查询连接条件

时间:2017-09-27 04:53:02

标签: mysql sql database join plsql

我有一个以下关系的数据库:

Bar(Name, Address, Licence)
Beer(Name,Manufacture)
Drinker(Name,Address)
Frequents(DrinkerName,BarName)
Likes(DrinkerName,BeerName)
Sells(BarName,BeerName,Amount)
Serves(BarName,BeerName)

示例DDL声明:

    CREATE TABLE `Bar` (
      `Name` varchar(255) NOT NULL,
      `Address` varchar(255) DEFAULT NULL,
      `Licence` varchar(255) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `Bar` (`Name`, `Address`, `Licence`) VALUES
    ('Deluxe', 'Luxvagen 2', 'Yes'),
    ('Grace', 'Gracevagen 2', 'Yes'),
    ('KrogBar', 'Barvagen 2', 'Yes');

   CREATE TABLE `Beer` (
      `Name` varchar(255) NOT NULL,
      `Manufacture` varchar(255) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `Beer` (`Name`, `Manufacture`) VALUES
    ('Carlsberg', 'Coppers'),
    ('Heiniken', 'Spendrups'),
    ('Miller', 'DaMill');

    CREATE TABLE `Boor` (
      `Name` varchar(255) NOT NULL,
      `Age` int(11) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    CREATE TABLE `Drinker` (
      `Name` varchar(255) NOT NULL,
      `Address` varchar(255) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `Drinker` (`Name`, `Address`) VALUES
    ('Alex', 'Överbar 2'),
    ('Bam', 'Påbar 2'),
    ('Emil', 'Mittibar 2'),
    ('Max', 'Ibar 2'),
    ('Petra', 'Förebar 2'),
    ('Rebecca', 'Efterbar 2'),
    ('Sam', 'Underbar 2');

    CREATE TABLE `Frequents` (
      `DrinkerName` varchar(255) NOT NULL DEFAULT '',
      `BarName` varchar(255) NOT NULL DEFAULT ''
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `Frequents` (`DrinkerName`, `BarName`) VALUES
    ('Emil', 'Deluxe'),
    ('Max', 'Deluxe'),
    ('Rebecca', 'Deluxe'),
    ('Alex', 'Grace'),
    ('Petra', 'Grace'),
    ('Bam', 'KrogBar'),
    ('Sam', 'KrogBar');

    CREATE TABLE `Likes` (
      `DrinkerName` varchar(255) NOT NULL DEFAULT '',
      `BeerName` varchar(255) NOT NULL DEFAULT ''
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `Likes` (`DrinkerName`, `BeerName`) VALUES
    ('Bam', 'Carlsberg'),
    ('Emil', 'Carlsberg'),
    ('Rebecca', 'Carlsberg'),
    ('Emil', 'Heiniken'),
    ('Max', 'Heiniken'),
    ('Petra', 'Heiniken'),
    ('Sam', 'Heiniken'),
    ('Alex', 'Miller');

    CREATE TABLE `Sells` (
      `BarName` varchar(100) DEFAULT NULL,
      `BeerName` varchar(100) DEFAULT NULL,
      `Amount` int(11) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `Sells` (`BarName`, `BeerName`, `Amount`) VALUES
    ('KrogBar', 'Miller', 3),
    ('KrogBar', 'Carlsberg', 2),
    ('KrogBar', 'Heiniken', 1),
    ('Deluxe', 'Heiniken', 1);

    CREATE TABLE `Serves` (
      `BarName` varchar(255) NOT NULL DEFAULT '',
      `BeerName` varchar(255) NOT NULL DEFAULT ''
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    INSERT INTO `Serves` (`BarName`, `BeerName`) VALUES
    ('Grace', 'Carlsberg'),
    ('KrogBar', 'Carlsberg'),
    ('Deluxe', 'Heiniken'),
    ('Grace', 'Heiniken'),
    ('KrogBar', 'Heiniken'),
    ('KrogBar', 'Miller');

我想找到那些经常只有自己喜欢的啤酒吧的饮酒者(假设每个饮酒者经常光顾至少一个酒吧)。 我该如何构建这样的查询?我知道我必须使用连接和子查询,我不是其中任何一个新手,但我的所有实现都没有产生正确的结果。

1 个答案:

答案 0 :(得分:0)

这就是你要找的东西;

Select DISTINCT bar.Name,bar.Address,frequents.DrinkerName, likes.BeerName
From
Bar
Join frequents on bar.Name = frequents.BarName
join sells on bar.name = sells.BarName
join likes on  frequents.DrinkerName = likes.DrinkerName

enter image description here