简体   繁体   中英

query to count MySQL rows

Trying to get a count (to later multiply with a rate) of names in upto (if data is present in one or more) four rows. In other words, there is a price per person occupying a room. So, I want to count the number of persons in the room by names entered on the form and saved in the database table.

I could simply add a field where the user also selects the number of people in addition to completeing the name fields but this seems redundant (and prone to error).

Setup

Table: 1. clients which has columns:

id, 
tourbk_id, 
tourstart, 
roomtype1, 
client1_name, 
client2_name, 
client3_name, 
client4_name

Question

I have a query which currently checks the roomtype to the per person price for that room type and is working to produce the result but, of course, it is only returning (for two people in a room) the price person for double occupancy.

Eg: (per person prices)... single = $10; double = $20; triple = $30; quad = $40

My current result for double room is $20 (which echo's next to "Price per person". I need a query to count the total persons in this double and multiple times the rate ... "Total: query[$20 * 2]"

How do I code a query to count the "client _name" entries in a table?

You ought to consider normalising your schema by having a separate client_names table that relates a single name column to a single booking identifier: multiple clients would then be represented by multiple records in that new table. You would count clients with:

SELECT COUNT(*) FROM client_names WHERE booking_id = ...

However, with your current structure (and assuming that the name columns are NULL if there is no such client), you could do:

SELECT (client1_name IS NOT NULL)
     + (client2_name IS NOT NULL)
     + (client3_name IS NOT NULL)
     + (client4_name IS NOT NULL)
    AS client_count
FROM   clients
-- etc.

Here I've added a virtual column named ClientCount which is the count of clients in the room. I like to use client1_name > '' because it works whether you use blanks or NULLs, and saves me from asking a question.

SELECT
    id, 
    tourbk_id, 
    tourstart, 
    roomtype1, 
    client1_name, 
    client2_name, 
    client3_name, 
    client4_name,
    CASE WHEN client1_name > '' THEN 1 ELSE 0 end +
    CASE WHEN client2_name > '' THEN 1 ELSE 0 end +
    CASE WHEN client3_name > '' THEN 1 ELSE 0 end +
    CASE WHEN client4_name > '' THEN 1 ELSE 0 end    ClientCount
FROM TBL

and, as a round-about way of doing it I thought of the following which also works (will just need to make a case for the 1, 2, 3, and 4 ["if roomtype1='sgl', then $mult=1, if roomtype='dbl', then $mult=2...] multiplyer to check for the other room types):

$total = ($roomrate['roomprice'] * 2);
     echo "Total this booking : ";
     echo $total;

thanks to all for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM