0

好的,我正在使用出版商​​数据库,我需要提出的查询是查找在每家商店出售过副本的任何书籍的标题。

我正在处理的主要表格是titlesalesstore

这是我的尝试

  SELECT title, titles.title_id, stores.stor_id, sales.stor_id, stor_name
    from titles
     FULL OUTER JOIN sales on sales.title_id = titles.title_id
    FULL OUTER JOIN stores on stores.stor_id = sales.stor_id
    --WHERE sales.stor_id = stores.stor_id AND sales.title_id = titles.title_id

我有一个想法我应该做什么。我需要加入titles.title_idsales.title_id然后检查是否sales.stor_name等于stores.stor_name

4

4 回答 4

2
select t.title_id, t.title
from titles as t
where (select count(*) from stores) 
    = (select count(distinct store_id) from sales where title_id=t.title_id)
于 2012-03-20T03:10:12.193 回答
2

试试这个:

select t1.title from titles t1
where t1.title_id not in (
  select sub.title_id from sales sa
  right join (
    select t.title_id, stor_id from titles t, stores st
  ) Sub on sa.title_id = sub.title_id and sa.stor_id = sub.stor_id
  where sa.title_id is null)

例子

于 2012-03-20T04:03:39.053 回答
1

您是否尝试过在 mysql 中使用“IN”。您的问题似乎缺乏详细信息,但据我了解,您需要使用“IN”功能

http://spin.atomicobject.com/2011/03/25/mysql-in-query-performance/

于 2012-03-20T03:13:29.187 回答
0

您的查询应类似于:

select titles
  from title, sales, store
 where (join conditions here)

连接条件应该是与 title 和 store 表以及 store 到 sales 表相关联的条件。例如,如果我的 title 表有一个 ISBN 字段,而我的 store 表有一个 ISBN 字段,则应包含title.isbn = store.isbn. 您将需要and为所有连接条件包括一个。

于 2012-03-20T03:14:07.807 回答