简体   繁体   English

查询以计数MySQL行

[英]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: 表:1.具有以下列的客户端

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; 例如:(每人价格)...单身= $ 10; double = $20; 双倍= $ 20; triple = $30; 三重= $ 30; quad = $40 四= $ 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]" 我当前的双人间价格为$ 20(这是“每人价格”旁边的回声。我需要一个查询来计算此倍数和倍率中的总人数...“总计:query [$ 20 * 2]”

How do I code a query to count the "client _name" entries in a table? 如何编写查询以对表中的“ client _name”条目进行计数?

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. 您应该考虑通过使用单独的client_names表来规范您的架构,该表将单个 name列与单个预订标识符相关联:然后,该新表中的多个记录将表示多个客户端。 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: 但是,使用当前结构(如果没有这样的客户端,并假设name列为NULL ),则可以执行以下操作:

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. 在这里,我添加了一个名为ClientCount的虚拟列,它是会议室中客户的数量。 I like to use client1_name > '' because it works whether you use blanks or NULLs, and saves me from asking a question. 我喜欢使用client1_name > ''因为无论您使用空格还是NULL,它都可以工作,并且使我免于提出问题。

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): 并且,因为这样做的一个迂回的办法,我认为这也适用以下的(将只需要保证的情况下为1,2,3,4 [“如果roomtype1 =‘SGL’,那么$ MULT = 1,如果roomtype ='dbl',则$ mult = 2 ...]乘数以检查其他房间类型):

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

thanks to all for the help! 感谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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