简体   繁体   中英

Write a SQL query to list the recording id and recording date of all recordings that appear on all of the cds in the cd table

I've probably tried writing this query 30 different ways, I and just can't seem to figure it out. Here is the problem:

"Write a SQL query to list the recording id and recording date of all recordings that appear on all of the cds in the cd table. Write the query using EXISTS.Columns should be rcdid and rcddate."

I can't for the life of me understand how to get this correctly. Every query I make just lists the entire recording table's rcdid and rcddate no matter what.

Here are the tables that would (or could) be helpful:

CREATE TABLE label (
  lbltitle varchar(50) not null,
  lblstreet varchar(50) not null,
  lblcity varchar(50) not null,
  lblstate char(2) not null,
  lblpostcode varchar(10) not null,
  lblnation char(3) not null,
    PRIMARY KEY(lbltitle));

CREATE TABLE cd (
  cdid integer not null,
  cdlblid varchar(50) not null,
  cdtitle varchar(100) not null,
  cdyear integer not null,
  lbltitle varchar(50) not null,
    PRIMARY KEY(cdid),
    CONSTRAINT FOREIGN KEY(lbltitle) REFERENCES label(lbltitle));

CREATE TABLE composition (
  compid integer not null,
  comptitle varchar(100) not null,
  compyear integer,
    PRIMARY KEY(compid));

CREATE TABLE recording (
  rcdid integer not null,
  rcdlength decimal(4,2) not null,
  rcddate date,
  compid integer not null,
    PRIMARY KEY(rcdid, compid),
    CONSTRAINT FOREIGN KEY(compid) REFERENCES composition(compid));

CREATE TABLE track (
  cdid integer not null,
  trknum integer not null,
  rcdid integer not null,
  compid integer not null,
    PRIMARY KEY(cdid, trknum),
    CONSTRAINT FOREIGN KEY(cdid) REFERENCES cd(cdid),
               FOREIGN KEY(rcdid, compid) REFERENCES recording(rcdid, compid));

Here is a diagram of the tables, if that helps: https://i.imgur.com/Qzr5EMl.png

And here are a couple of my many attempts:

SELECT rcdid, rcddate FROM recording
 WHERE EXISTS (
    SELECT * FROM cd JOIN track ON cd.cdid = track.cdid);

SELECT recording.rcdid, rcddate FROM recording JOIN track
  ON track.rcdid = recording.rcdid
  WHERE EXISTS (
    SELECT * FROM cd JOIN track on cd.cdid = track.cdid WHERE track.rcdid = recording.rcdid);

Like I said, every query I try pulls the info from every row in the recording table, and I am at a total loss.

You need a Correlated Subquery between recording and track :

select rcdid, rcddate
from recording as r
where exists
 ( select *
   from track as t
   where t.rcdid  = r.rcdid
     and t.compid = r.compid
 )

No need to add cd because there's a Foreign Key between track and cd

If the intention is to look for recordings that appear in ALL the cd's.

Then the count of CD's for that recording id should be the same as the count of all the CD's.

So perhaps something like this:

select rcdid, rcddate
from recording rcd
cross join (select count(cdid) as total from cd) cd
where exists 
(
  select 1
  from track tr
  where tr.rcdid = rcd.rcdid
  group by tr.rcdid
  having count(distinct tr.cdid) = cd.total
);

A test on rextester here

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