简体   繁体   中英

SQL query join condition

I have a database of the following relations:

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

The Sample DDL Statements:

    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');

I want to find drinkers that frequent only bars that serve beer that they like (the assumption is that each drinker frequents at least one bar). How can I construct such a query? I know that I have to use Joins and Sub-queries, I am not new to either of them but all my implementations have not yielded the correct results.

Is this what you looking for;

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM