doujing2497 2015-03-19 14:58
浏览 10
已采纳

SQL查询 - 有条款

Here are my database relations:

shows(showID, title, premiere_year, network, creator, category)

episode(showID, episodeID, airdate, title)

  • showID is a foreign key to shows

actor(actID, fname, lname)

  • main_cast(showID, actorID, role)
  • showID is a foreign key to shows
  • actID is a foreign key to actor

recurring_cast(showID, episodeID, actorID, role)

  • showID is a foreign key to shows
  • episodeID is a foreign key to episode
  • actID is a foreign key to actor

customer(custID, fname, lname, email, creditcard,membersince,renewaldate, password, username)

cust_queue(custID, showID, datequeued)

  • custID is a foreign key to customer
  • showID is a foreign key to shows

watched(custID, showID, episodeID, datewatched) - custID is a foreign key to customer - showID is a foreign key to shows - (showID, episodeID) is a foreign key to episode - (custID, showID) is a foreign key to cust_queue

All the 'IDs' are primary keys


I was given some queries and I'm having problems with them. One of which involves a HAVING Clause. The query is:

Find how many shows have episodes with the word "good" in the title?

Im assuming I have to use a COUNT and a HAVING Clause. But my syntax is always off. Someone help?!! My latest attempt looks like this:

SELECT Shows, COUNT(Episode)
FROM Shows, Episode
WHERE Shows.showid = Episode.showID
GROUP BY Shows
HAVING (Episode.title = 'good');
  • 写回答

1条回答 默认 最新

  • doraemon0769 2015-03-19 15:13
    关注

    You need to use 'LIKE' and then wild cards

    ... WHERE title LIKE '%good%'
    

    The % at each end will find any titles with the word 'good' included.

    For your query in particular, the query below should find all episodes with the word 'good' if I understand your database schematic correctly

    SELECT COUNT(*) FROM Episode WHERE title LIKE '%good%'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)