如何编写这个JOIN查询?没有公共表数据



我需要显示以超过$20的价格出售商品的人的姓和名,我需要连接title和author表,但它们没有任何公共键?我该怎么做呢?我只需要显示他们的名字作为输入,这是我到目前为止,我得到空白的结果。我只是在学习,所以尽量不要太专业

列名称如下:

authors: pk- au_id
             au_fname
             au_lname
titles: pk- title_id
            title   
            price
        fk- pub_id

这是我目前为止写的:

select price, au_lname, au_fname
from titles JOIN authors
on authors.au_id=titles.pub_id

看起来您也有一个titleauthor表。这将有助于示例代码中的查询。但是,在这种情况下不需要这个表,因为您的问题实际上是在询问销售人员,而不是作者。你真的需要这样的东西:

select distinct e.firstname, e.lastname
from sales s
inner join employee e on e.empl_id = s.empl_id
where s.itemprice >= 20

当然,我不得不胡乱猜测这里的列名。您可以将其扩展为包含有关这些项目的标题和作者的信息,如下所示:

select distinct e.firstname as SalePersonFirstName, e.lastname as SalesPersonLastName
    , t.Title, a.Lastname As AuthorLastName, a.firstname as FirstName
    , s.itemprice
from sales s
inner join employee e on e.empl_id = s.empl_id
inner join titles t on t.title_id = sales.title_id
inner join titleauthors ta on ta.title_id = t.title_id
inner join authors a on a.au_id = ta.au_id
where s.itemprice >= 20

最新更新