我在MySQL查詢時遇到了問題(我是一個新手,所以對我來說很簡單!)。我有一個帶有電視節目的網站,並希望根據兩種不同的操作(用於自定義時間表)選擇兩個日期之間播出的節目(沒有問題)。基本上,如果用戶要麼跟隨(uses_follow_shows)或觀看節目(user_watched),我想要顯示該節目,所以我想我會有(query_on_follow或query_on_watch)像下面但不起作用(我得到1200行而不是3,它從user_watched獲取每個現有行)。在不同的表上加入兩個類似的查詢 - MySQL OR
我認爲這種說法會做,但它不,我不明白爲什麼
SELECT * FROM show_episode_airdate, show_episode, show_network, network, shows, users_follow_shows, user_watched
WHERE show_episode_airdate.airdate BETWEEN '2013-07-20' AND '2013-07-27'
AND (show_episode.episode_id = show_episode_airdate.episode_id
AND shows.id = show_network.show_id
AND show_network.network_id = network.network_id
AND show_episode.imdb_id_show = shows.imdb_id
AND users_follow_shows.user_id = 2 AND shows.id = users_follow_shows.show_id
OR user_watched.user_id = 2
AND shows.id = user_watched.show_id
AND show_episode.episode_id = show_episode_airdate.episode_id
AND shows.id = show_network.show_id
AND show_network.network_id = network.network_id
AND show_episode.imdb_id_show = shows.imdb_id)
ORDER by network.network_id ASC
誰能告訴我有什麼不對?
謝謝!
編輯:
SELECT *
FROM show_episode_airdate join
show_episode
on show_episode.episode_id = show_episode_airdate.episode_id join
shows
on shows.imdb_id = show_episode.imdb_id_show join
show_network
on show_network.show_id = shows.id join
network
on show_network.network_id = network.network_id join
users_follow_shows
on shows.id = users_follow_shows.show_id join
user_watched
on shows.id = user_watched.show_id
WHERE show_episode_airdate.airdate BETWEEN '2013-07-20' AND '2013-07-27' and
(users_follow_shows.user_id = 2 or
user_watched.user_id = 2
)
ORDER by network.network_id ASC;
仍然返回1200行。我認爲這是因爲在第一個OR中,它選擇了user_watched和第二個,user_follow_shows中的所有內容...不是? – Callombert
我認爲@Gordon Linoff解釋說它是最好的方式:D +懶得寫全部東西,所以請閱讀:D – mirkobrankovic
好吧!謝謝你的時間。 – Callombert