4

Найти какие тарифы доступны каким клиентам

SELECT
    clients.id AS clientId,
    prices.id AS tariffId,
    prices.name AS tariffName
FROM
    ((prices
    JOIN tariffs ON ((prices.id = tariffs.idPrice)))
    JOIN clients ON ((tariffs.isMember = clients.isMember)))

Выбрать резервы клиента

select startTime, courtNo, duration from reservesView where idClient=id;

Выбрать все резервы с именем клиента

SELECT
    reserves.idClient AS idClient,
    clients.name AS clientName,
    reserves.startTime AS startTime,
    reserves.idCourt AS courtNo,
    reserves.duration AS duration
FROM
    (reserves
    JOIN clients ON ((reserves.idClient = clients.id)))

и ценой резерва

SELECT
    reserves.idClient AS idClient,
    clients.name AS clientName,
    reserves.startTime AS startTime,
    reserves.idCourt AS courtNo,
    reserves.duration AS duration,
    prices.price AS pricePerHour,
    prices.price*TIME_TO_SEC(duration)/3600 AS totalPrice
FROM
    reserves
    JOIN clients ON reserves.idClient = clients.id
    JOIN tariffs ON reserves.idCourt = tariffs.idCourt AND clients.isMember = tariffs.isMember
    JOIN prices ON tariffs.idPrice = prices.id;

Приход от клиентов в текущем месяце

SELECT
    reserves.idClient AS idClient,
    clients.name AS name,
    SUM(prices.price*TIME_TO_SEC(duration)/3600) AS income
FROM
    reserves
    JOIN clients ON clients.id = reserves.idClient
    JOIN tariffs ON clients.isMember = tariffs.isMember
        AND reserves.idCourt = tariffs.idCourt
    JOIN prices ON tariffs.idPrice = prices.id
WHERE MONTH(startTime) = MONTH(NOW())
GROUP BY reserves.idClient;