Subquery returns multiple rows in HAVING clause
I want to get the customer who mostly borrowed films of category 3 in 2016, July
SELECT c_firstName, c_lastName, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID HAVING rental.c_ID=MAX((SELECT COUNT(rental.c_ID)
FROM customer, copies, rentalprocess, rental, film
WHERE customer.c_ID=rental.c_ID AND rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%" GROUP BY rental.c_ID))
But ir doesn't work because it said that the subquery returns more than one row
What can I do?
mysql sql subquery having
add a comment |
I want to get the customer who mostly borrowed films of category 3 in 2016, July
SELECT c_firstName, c_lastName, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID HAVING rental.c_ID=MAX((SELECT COUNT(rental.c_ID)
FROM customer, copies, rentalprocess, rental, film
WHERE customer.c_ID=rental.c_ID AND rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%" GROUP BY rental.c_ID))
But ir doesn't work because it said that the subquery returns more than one row
What can I do?
mysql sql subquery having
1
What rmdbs is this? Oracle, MySQL etc?
– EoinS
Nov 14 '18 at 19:18
3
Never use commas in theFROM
clause. Always use proper, explicit, standardJOIN
syntax.
– Gordon Linoff
Nov 14 '18 at 19:19
It is MySQL RMDBS
– Grevak
Nov 14 '18 at 19:20
1
RDBMS, relational database management system :)
– Caius Jard
Nov 14 '18 at 20:05
1
Use properJOIN
. It's been around for over 20 years.
– Eric
Nov 14 '18 at 23:55
add a comment |
I want to get the customer who mostly borrowed films of category 3 in 2016, July
SELECT c_firstName, c_lastName, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID HAVING rental.c_ID=MAX((SELECT COUNT(rental.c_ID)
FROM customer, copies, rentalprocess, rental, film
WHERE customer.c_ID=rental.c_ID AND rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%" GROUP BY rental.c_ID))
But ir doesn't work because it said that the subquery returns more than one row
What can I do?
mysql sql subquery having
I want to get the customer who mostly borrowed films of category 3 in 2016, July
SELECT c_firstName, c_lastName, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID HAVING rental.c_ID=MAX((SELECT COUNT(rental.c_ID)
FROM customer, copies, rentalprocess, rental, film
WHERE customer.c_ID=rental.c_ID AND rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%" GROUP BY rental.c_ID))
But ir doesn't work because it said that the subquery returns more than one row
What can I do?
mysql sql subquery having
mysql sql subquery having
edited Nov 14 '18 at 21:43
a_horse_with_no_name
304k46465561
304k46465561
asked Nov 14 '18 at 19:08
GrevakGrevak
11210
11210
1
What rmdbs is this? Oracle, MySQL etc?
– EoinS
Nov 14 '18 at 19:18
3
Never use commas in theFROM
clause. Always use proper, explicit, standardJOIN
syntax.
– Gordon Linoff
Nov 14 '18 at 19:19
It is MySQL RMDBS
– Grevak
Nov 14 '18 at 19:20
1
RDBMS, relational database management system :)
– Caius Jard
Nov 14 '18 at 20:05
1
Use properJOIN
. It's been around for over 20 years.
– Eric
Nov 14 '18 at 23:55
add a comment |
1
What rmdbs is this? Oracle, MySQL etc?
– EoinS
Nov 14 '18 at 19:18
3
Never use commas in theFROM
clause. Always use proper, explicit, standardJOIN
syntax.
– Gordon Linoff
Nov 14 '18 at 19:19
It is MySQL RMDBS
– Grevak
Nov 14 '18 at 19:20
1
RDBMS, relational database management system :)
– Caius Jard
Nov 14 '18 at 20:05
1
Use properJOIN
. It's been around for over 20 years.
– Eric
Nov 14 '18 at 23:55
1
1
What rmdbs is this? Oracle, MySQL etc?
– EoinS
Nov 14 '18 at 19:18
What rmdbs is this? Oracle, MySQL etc?
– EoinS
Nov 14 '18 at 19:18
3
3
Never use commas in the
FROM
clause. Always use proper, explicit, standard JOIN
syntax.– Gordon Linoff
Nov 14 '18 at 19:19
Never use commas in the
FROM
clause. Always use proper, explicit, standard JOIN
syntax.– Gordon Linoff
Nov 14 '18 at 19:19
It is MySQL RMDBS
– Grevak
Nov 14 '18 at 19:20
It is MySQL RMDBS
– Grevak
Nov 14 '18 at 19:20
1
1
RDBMS, relational database management system :)
– Caius Jard
Nov 14 '18 at 20:05
RDBMS, relational database management system :)
– Caius Jard
Nov 14 '18 at 20:05
1
1
Use proper
JOIN
. It's been around for over 20 years.– Eric
Nov 14 '18 at 23:55
Use proper
JOIN
. It's been around for over 20 years.– Eric
Nov 14 '18 at 23:55
add a comment |
2 Answers
2
active
oldest
votes
Max()
is an aggregate function that needs to be in a select statement
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM customer, copies, rentalprocess, rental, film
WHERE
customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
In this case the sub-select returns multiple rows but then you take the max value of that query
Comment from Mr Linoff is correct, you should use explicity joins:
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM
customer
inner join rental
on customer.c_ID=rental.c_ID
inner join rentalprocess
on rentalprocess.r_ID=rental.r_ID
inner join copies
on rentalprocess.s_ID=copies.s_ID
inner join film on film.f_ID=copies.f_ID
WHERE
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
Now it says Every derived table must have its own alias
– Grevak
Nov 14 '18 at 19:27
So give each subquery an alias.. (Which would be a lot easier if the indentation on that SQL wasn't all over the place)
– Caius Jard
Nov 14 '18 at 20:15
@CaiusJard mobile submission :(
– EoinS
Nov 14 '18 at 23:41
@Grevak updated, thanks!
– EoinS
Nov 14 '18 at 23:44
1
@EoinS yes, sorry I should have made it clear that my comment was directed to Grevak - on the scale of things “subquery needs an alias” is one of those errors/minor typos I would expect an OP to sort out for themselves because it basically involves finding a bracket and putting a letter A after it.. I’ve always had a concern that asking a question here becomes a sort of mental resignation where people are no longer prepared to try debugging anything related to the problem and expect to be delivered a perfect solution :/ Kudos on persevering with mobile SO; I do too and its a pain in the ass :)
– Caius Jard
Nov 15 '18 at 5:42
|
show 1 more comment
your code should look something like this, join the tables properly in the code.
I dont know wich columns and table would suit best for the solution becuse I dont got your full schema. but this should give faster query. Put more columns in the select if you wish.
select c_firstName | ' ' | c_lastName, count(rental.c_ID) as rentalCustomer
from customer
inner join rental
on join " connect the both tables"
innner join rentalprocess
on "connect rental with rentalprocess"
inner join copies
on " connect rentalprocess with copies"
inner join film
on "connect copies with film"
WHERE customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%"
group by c_firstName, c_lastName, rental.c_ID
order by rental.c_ID desc;
If you’re going to this extent, you could go the whole way and pull the join ON conditions out of the WHERE clause - that’s where they were in the 1980s version posted originally!:)
– Caius Jard
Nov 15 '18 at 5:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307179%2fsubquery-returns-multiple-rows-in-having-clause%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Max()
is an aggregate function that needs to be in a select statement
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM customer, copies, rentalprocess, rental, film
WHERE
customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
In this case the sub-select returns multiple rows but then you take the max value of that query
Comment from Mr Linoff is correct, you should use explicity joins:
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM
customer
inner join rental
on customer.c_ID=rental.c_ID
inner join rentalprocess
on rentalprocess.r_ID=rental.r_ID
inner join copies
on rentalprocess.s_ID=copies.s_ID
inner join film on film.f_ID=copies.f_ID
WHERE
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
Now it says Every derived table must have its own alias
– Grevak
Nov 14 '18 at 19:27
So give each subquery an alias.. (Which would be a lot easier if the indentation on that SQL wasn't all over the place)
– Caius Jard
Nov 14 '18 at 20:15
@CaiusJard mobile submission :(
– EoinS
Nov 14 '18 at 23:41
@Grevak updated, thanks!
– EoinS
Nov 14 '18 at 23:44
1
@EoinS yes, sorry I should have made it clear that my comment was directed to Grevak - on the scale of things “subquery needs an alias” is one of those errors/minor typos I would expect an OP to sort out for themselves because it basically involves finding a bracket and putting a letter A after it.. I’ve always had a concern that asking a question here becomes a sort of mental resignation where people are no longer prepared to try debugging anything related to the problem and expect to be delivered a perfect solution :/ Kudos on persevering with mobile SO; I do too and its a pain in the ass :)
– Caius Jard
Nov 15 '18 at 5:42
|
show 1 more comment
Max()
is an aggregate function that needs to be in a select statement
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM customer, copies, rentalprocess, rental, film
WHERE
customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
In this case the sub-select returns multiple rows but then you take the max value of that query
Comment from Mr Linoff is correct, you should use explicity joins:
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM
customer
inner join rental
on customer.c_ID=rental.c_ID
inner join rentalprocess
on rentalprocess.r_ID=rental.r_ID
inner join copies
on rentalprocess.s_ID=copies.s_ID
inner join film on film.f_ID=copies.f_ID
WHERE
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
Now it says Every derived table must have its own alias
– Grevak
Nov 14 '18 at 19:27
So give each subquery an alias.. (Which would be a lot easier if the indentation on that SQL wasn't all over the place)
– Caius Jard
Nov 14 '18 at 20:15
@CaiusJard mobile submission :(
– EoinS
Nov 14 '18 at 23:41
@Grevak updated, thanks!
– EoinS
Nov 14 '18 at 23:44
1
@EoinS yes, sorry I should have made it clear that my comment was directed to Grevak - on the scale of things “subquery needs an alias” is one of those errors/minor typos I would expect an OP to sort out for themselves because it basically involves finding a bracket and putting a letter A after it.. I’ve always had a concern that asking a question here becomes a sort of mental resignation where people are no longer prepared to try debugging anything related to the problem and expect to be delivered a perfect solution :/ Kudos on persevering with mobile SO; I do too and its a pain in the ass :)
– Caius Jard
Nov 15 '18 at 5:42
|
show 1 more comment
Max()
is an aggregate function that needs to be in a select statement
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM customer, copies, rentalprocess, rental, film
WHERE
customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
In this case the sub-select returns multiple rows but then you take the max value of that query
Comment from Mr Linoff is correct, you should use explicity joins:
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM
customer
inner join rental
on customer.c_ID=rental.c_ID
inner join rentalprocess
on rentalprocess.r_ID=rental.r_ID
inner join copies
on rentalprocess.s_ID=copies.s_ID
inner join film on film.f_ID=copies.f_ID
WHERE
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
Max()
is an aggregate function that needs to be in a select statement
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM customer, copies, rentalprocess, rental, film
WHERE
customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
In this case the sub-select returns multiple rows but then you take the max value of that query
Comment from Mr Linoff is correct, you should use explicity joins:
SELECT
c_firstName
, c_lastName
, rental.c_ID
FROM customer, rental
GROUP BY rental.c_ID
HAVING rental.c_ID=
(
select
MAX(i.iID)
from
(
SELECT
COUNT(rental.c_ID) iID
FROM
customer
inner join rental
on customer.c_ID=rental.c_ID
inner join rentalprocess
on rentalprocess.r_ID=rental.r_ID
inner join copies
on rentalprocess.s_ID=copies.s_ID
inner join film on film.f_ID=copies.f_ID
WHERE
f_category=3
AND r_date LIKE "2016-07%"
GROUP BY rental.c_ID
) i
)
edited Nov 14 '18 at 23:49
answered Nov 14 '18 at 19:17
EoinSEoinS
3,6401923
3,6401923
Now it says Every derived table must have its own alias
– Grevak
Nov 14 '18 at 19:27
So give each subquery an alias.. (Which would be a lot easier if the indentation on that SQL wasn't all over the place)
– Caius Jard
Nov 14 '18 at 20:15
@CaiusJard mobile submission :(
– EoinS
Nov 14 '18 at 23:41
@Grevak updated, thanks!
– EoinS
Nov 14 '18 at 23:44
1
@EoinS yes, sorry I should have made it clear that my comment was directed to Grevak - on the scale of things “subquery needs an alias” is one of those errors/minor typos I would expect an OP to sort out for themselves because it basically involves finding a bracket and putting a letter A after it.. I’ve always had a concern that asking a question here becomes a sort of mental resignation where people are no longer prepared to try debugging anything related to the problem and expect to be delivered a perfect solution :/ Kudos on persevering with mobile SO; I do too and its a pain in the ass :)
– Caius Jard
Nov 15 '18 at 5:42
|
show 1 more comment
Now it says Every derived table must have its own alias
– Grevak
Nov 14 '18 at 19:27
So give each subquery an alias.. (Which would be a lot easier if the indentation on that SQL wasn't all over the place)
– Caius Jard
Nov 14 '18 at 20:15
@CaiusJard mobile submission :(
– EoinS
Nov 14 '18 at 23:41
@Grevak updated, thanks!
– EoinS
Nov 14 '18 at 23:44
1
@EoinS yes, sorry I should have made it clear that my comment was directed to Grevak - on the scale of things “subquery needs an alias” is one of those errors/minor typos I would expect an OP to sort out for themselves because it basically involves finding a bracket and putting a letter A after it.. I’ve always had a concern that asking a question here becomes a sort of mental resignation where people are no longer prepared to try debugging anything related to the problem and expect to be delivered a perfect solution :/ Kudos on persevering with mobile SO; I do too and its a pain in the ass :)
– Caius Jard
Nov 15 '18 at 5:42
Now it says Every derived table must have its own alias
– Grevak
Nov 14 '18 at 19:27
Now it says Every derived table must have its own alias
– Grevak
Nov 14 '18 at 19:27
So give each subquery an alias.. (Which would be a lot easier if the indentation on that SQL wasn't all over the place)
– Caius Jard
Nov 14 '18 at 20:15
So give each subquery an alias.. (Which would be a lot easier if the indentation on that SQL wasn't all over the place)
– Caius Jard
Nov 14 '18 at 20:15
@CaiusJard mobile submission :(
– EoinS
Nov 14 '18 at 23:41
@CaiusJard mobile submission :(
– EoinS
Nov 14 '18 at 23:41
@Grevak updated, thanks!
– EoinS
Nov 14 '18 at 23:44
@Grevak updated, thanks!
– EoinS
Nov 14 '18 at 23:44
1
1
@EoinS yes, sorry I should have made it clear that my comment was directed to Grevak - on the scale of things “subquery needs an alias” is one of those errors/minor typos I would expect an OP to sort out for themselves because it basically involves finding a bracket and putting a letter A after it.. I’ve always had a concern that asking a question here becomes a sort of mental resignation where people are no longer prepared to try debugging anything related to the problem and expect to be delivered a perfect solution :/ Kudos on persevering with mobile SO; I do too and its a pain in the ass :)
– Caius Jard
Nov 15 '18 at 5:42
@EoinS yes, sorry I should have made it clear that my comment was directed to Grevak - on the scale of things “subquery needs an alias” is one of those errors/minor typos I would expect an OP to sort out for themselves because it basically involves finding a bracket and putting a letter A after it.. I’ve always had a concern that asking a question here becomes a sort of mental resignation where people are no longer prepared to try debugging anything related to the problem and expect to be delivered a perfect solution :/ Kudos on persevering with mobile SO; I do too and its a pain in the ass :)
– Caius Jard
Nov 15 '18 at 5:42
|
show 1 more comment
your code should look something like this, join the tables properly in the code.
I dont know wich columns and table would suit best for the solution becuse I dont got your full schema. but this should give faster query. Put more columns in the select if you wish.
select c_firstName | ' ' | c_lastName, count(rental.c_ID) as rentalCustomer
from customer
inner join rental
on join " connect the both tables"
innner join rentalprocess
on "connect rental with rentalprocess"
inner join copies
on " connect rentalprocess with copies"
inner join film
on "connect copies with film"
WHERE customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%"
group by c_firstName, c_lastName, rental.c_ID
order by rental.c_ID desc;
If you’re going to this extent, you could go the whole way and pull the join ON conditions out of the WHERE clause - that’s where they were in the 1980s version posted originally!:)
– Caius Jard
Nov 15 '18 at 5:49
add a comment |
your code should look something like this, join the tables properly in the code.
I dont know wich columns and table would suit best for the solution becuse I dont got your full schema. but this should give faster query. Put more columns in the select if you wish.
select c_firstName | ' ' | c_lastName, count(rental.c_ID) as rentalCustomer
from customer
inner join rental
on join " connect the both tables"
innner join rentalprocess
on "connect rental with rentalprocess"
inner join copies
on " connect rentalprocess with copies"
inner join film
on "connect copies with film"
WHERE customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%"
group by c_firstName, c_lastName, rental.c_ID
order by rental.c_ID desc;
If you’re going to this extent, you could go the whole way and pull the join ON conditions out of the WHERE clause - that’s where they were in the 1980s version posted originally!:)
– Caius Jard
Nov 15 '18 at 5:49
add a comment |
your code should look something like this, join the tables properly in the code.
I dont know wich columns and table would suit best for the solution becuse I dont got your full schema. but this should give faster query. Put more columns in the select if you wish.
select c_firstName | ' ' | c_lastName, count(rental.c_ID) as rentalCustomer
from customer
inner join rental
on join " connect the both tables"
innner join rentalprocess
on "connect rental with rentalprocess"
inner join copies
on " connect rentalprocess with copies"
inner join film
on "connect copies with film"
WHERE customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%"
group by c_firstName, c_lastName, rental.c_ID
order by rental.c_ID desc;
your code should look something like this, join the tables properly in the code.
I dont know wich columns and table would suit best for the solution becuse I dont got your full schema. but this should give faster query. Put more columns in the select if you wish.
select c_firstName | ' ' | c_lastName, count(rental.c_ID) as rentalCustomer
from customer
inner join rental
on join " connect the both tables"
innner join rentalprocess
on "connect rental with rentalprocess"
inner join copies
on " connect rentalprocess with copies"
inner join film
on "connect copies with film"
WHERE customer.c_ID=rental.c_ID AND
rentalprocess.r_ID=rental.r_ID AND
rentalprocess.s_ID=copies.s_ID AND
film.f_ID=copies.f_ID AND
f_category=3 AND r_date LIKE "2016-07%"
group by c_firstName, c_lastName, rental.c_ID
order by rental.c_ID desc;
answered Nov 14 '18 at 21:33
friendfriend
1
1
If you’re going to this extent, you could go the whole way and pull the join ON conditions out of the WHERE clause - that’s where they were in the 1980s version posted originally!:)
– Caius Jard
Nov 15 '18 at 5:49
add a comment |
If you’re going to this extent, you could go the whole way and pull the join ON conditions out of the WHERE clause - that’s where they were in the 1980s version posted originally!:)
– Caius Jard
Nov 15 '18 at 5:49
If you’re going to this extent, you could go the whole way and pull the join ON conditions out of the WHERE clause - that’s where they were in the 1980s version posted originally!:)
– Caius Jard
Nov 15 '18 at 5:49
If you’re going to this extent, you could go the whole way and pull the join ON conditions out of the WHERE clause - that’s where they were in the 1980s version posted originally!:)
– Caius Jard
Nov 15 '18 at 5:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307179%2fsubquery-returns-multiple-rows-in-having-clause%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What rmdbs is this? Oracle, MySQL etc?
– EoinS
Nov 14 '18 at 19:18
3
Never use commas in the
FROM
clause. Always use proper, explicit, standardJOIN
syntax.– Gordon Linoff
Nov 14 '18 at 19:19
It is MySQL RMDBS
– Grevak
Nov 14 '18 at 19:20
1
RDBMS, relational database management system :)
– Caius Jard
Nov 14 '18 at 20:05
1
Use proper
JOIN
. It's been around for over 20 years.– Eric
Nov 14 '18 at 23:55