Select COUNT () from multiple databases in SQL

I am trying to return the number of clients in a certain state who have rented a particular movie, where the annuity table contains two columns, one for the client identifier and one for the film identifier. The function takes the movie ID and status and returns an integer with the number of clients.

Now I have an implementation, but it counts the number of rows that the entire query returns:

SELECT COUNT(*) as numCustomers FROM CUSTOMER C, RENTS R WHERE C.ST = '" + state + "' AND R.mid = " + movieID

And then I count the number of rows. I would just check numCustomers for the correct data. Thanks!

+3
source share
3 answers

-, RENTS CUSTOMER CustomerId?

-, INNER JOIN FROM, .

-, sql , , SQL Injection.

, SQL, , .

DECLARE @movieId int
DECLARE @state varchar(2)

SET @movieId = 12345
SET @state = 'NY'

SELECT
    COUNT(DISTINCT C.CustomerID) as numCustomers
FROM
    CUSTOMER C
INNER JOIN
    RENTS R
ON
    C.CustomerID = R.CustomerId
WHERE
    C.ST = @state
AND
    R.mid = @movieId
+8

- ( RENTS CUSTOMER):

SELECT COUNT(*) as numCustomers
FROM CUSTOMER c
WHERE
    c.ST = @State
    AND EXISTS
    (
        SELECT *
        FROM RENTS r
        WHERE r.CustomerID = c.CustomerID
        AND r.mid = @movieID
    )

, SQL-, .

+1

, .

: SELECT COUNT(C.ID) AS numCustomers FROM CUSTOMER C, RENTS R WHERE C.ID = R.RenterID AND C.ST = '" + state + "' AND R.mid = " + movieID

0

Source: https://habr.com/ru/post/1705201/


All Articles