mysql从count null减去
我有一个电影租赁数据库,我必须计算出有多少个给定电影的副本。 我有3张桌子:
a)“电影” -id_movie PK, -姓名, -copies_num
b)“客户端” -id_client PK, -client_name
c)“租金” movie_id pk fk, client_id pk fk, lental_date, MOVIE_RETURN_DATE,
所以我想,如果Movie_return_date为null,则意味着1份被租用,这意味着我有 (copies_num- count(电影返回日期的零),但我无法弄清楚如何做这样的选择查询
选择独特的m.title,m.copies_num as'max_num',m.copies_num -count -count -ifnull(ifnull(r.movie_return_date) ,null))作为租赁的可用r左r JOAN MOVING M.ID_MOVIE = R.MOVIE_ID Group by M.Copies_num,M.Title;
此类有效,但它从错误的电影中征服没有任何空
I have a movie rental Database where I have to calculate how many copies of a given movie I have available.
I Have 3 tables:
a) "Movies"
-Id_Movie PK,
-Name,
-Copies_num
b) "Client"
-Id_Client PK,
-Client_Name
c)"rentals"
Movie_Id PK FK,
Client_ID PK FK,
Rental_Date,
Movie_return_date
So I thought, if the movie_return_date is null that means 1 copy is rented, so that means i have
(Copies_num - COUNT(NULLS of movie return date) but i cant quite figure how to do such SELECT query
SELECT Distinct M.Title, M.Copies_num as 'max_num',M.Copies_num - COUNT(IFNULL(R.Movie_return_date , NULL)) as avalible FROM rentals R LEFT JOIN Movies M ON M.Id_Movie = R.Movie_ID GROUP BY M.Copies_num, M.Title;
This kinda works but it subtracks from the wrong movie, the one that does not have any nulls
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的问题,您可以尝试这样的事情:
If I understood your question correctly, you could try something like this: