Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php sql查询如何计算出一天中的最高收入者_Php_Mysql - Fatal编程技术网

Php sql查询如何计算出一天中的最高收入者

Php sql查询如何计算出一天中的最高收入者,php,mysql,Php,Mysql,我目前正在为某个特定的日期与造型师周旋,试图找出哪一个最赚钱。我正在努力超越我要达到的目标。我已经循环了我的问题,但现在我不知道该怎么办。一旦我有了我的价格数组,我如何按需要订购 foreach ($staffArray as $staff) { $query = sprintf( "SELECT SUM(bookingPrice) as price FROM booking WHERE i

我目前正在为某个特定的日期与造型师周旋,试图找出哪一个最赚钱。我正在努力超越我要达到的目标。我已经循环了我的问题,但现在我不知道该怎么办。一旦我有了我的价格数组,我如何按需要订购

foreach ($staffArray as $staff) {
    $query = sprintf(
               "SELECT SUM(bookingPrice) as price 
                FROM booking 
                WHERE idsystem=%s AND 
                      idbranch=%s AND 
                      bookingDate=%s AND 
                      idStatus=%s AND 
                      idstylist=%s",
               $this->db->GetSQLValueString($this->mysystem, "int"),
               $this->db->GetSQLValueString($this->branch, "int"),
               $this->db->GetSQLValueString($this->dateToday, "date"),
               $this->db->GetSQLValueString('1', "int"),
               $this->db->GetSQLValueString($staff, "int")
               );
    $result = $this->db->query($query); 
    $row = $this->db->fetch_assoc($result);
}

要使此特定代码正常工作,您可以执行以下操作:

foreach ($staffArray as $staff) {
    $query = sprintf(
               "SELECT SUM(bookingPrice) as price 
                FROM booking 
                WHERE idsystem=%s AND 
                      idbranch=%s AND 
                      bookingDate=%s AND 
                      idStatus=%s AND 
                      idstylist=%s",
               $this->db->GetSQLValueString($this->mysystem, "int"),
               $this->db->GetSQLValueString($this->branch, "int"),
               $this->db->GetSQLValueString($this->dateToday, "date"),
               $this->db->GetSQLValueString('1', "int"),
               $this->db->GetSQLValueString($staff, "int")
               );
    $result = $this->db->query($query); 
    $row = $this->db->fetch_assoc($result);
}
$prices = array(); // here we're going to store your data
foreach ($staffArray as $staff) {
    $query = sprintf("...", ...); 
    $result = $this->db->query($query);
    $row = $this->db->fetch_assoc($result);
    $prices[/* name of stylist */] = /* value to sort on */;
}
asort($prices); // Sort the array, keep the keys
foreach ($prices as $stylist => $price) {
    echo "$stylist: $price";
}

但是,最好对所有内容使用一个查询而不是foreach循环,让MySQL而不是PHP进行排序,并使用准备好的语句而不是sprintf

使用真正准备好的语句,而不是sprintf解决方案。另外,当将所有$staff作为一个查询而不是单独查询时,您可能会节省一些时间。如果您还提到更好的解决方案,为什么只给出更差的解决方案?@Sirko从问题中可以看出,OP在数组方面存在一些问题。我认为他还没有达到更复杂的SQL查询和预处理语句的水平,尽管我可能弄错了。我不会给他密码,如果它不能教会他一些东西。无论如何,如何使用准备好的语句已经在其他地方进行了广泛的讨论。@CamilStaps您的正确答案我从未使用或听说过准备好的语句。看来我还有点书要读do@DanielRobinson准备好的陈述很重要,但只要你只是玩玩就行。如果你是在一个专业的环境中,或者如果你认为人们会使用你的系统,这是必要的——但是如果你只是想学习PHP,这不是很好,但不是必要的。@CamilStaps谢谢你,伙计。这实际上是我建立的一个庞大的系统,目前正在沙龙中运行。我现在看到许多地方,事先准备好的声明会派上用场。我仍然没有真正得到它们,所以在我得到它们之前,我不会改变任何东西。我现在只能错过这份报告,直到我明白如何写这些以及他们在做什么。
foreach ($staffArray as $staff) {
    $query = sprintf(
               "SELECT SUM(bookingPrice) as price 
                FROM booking 
                WHERE idsystem=%s AND 
                      idbranch=%s AND 
                      bookingDate=%s AND 
                      idStatus=%s AND 
                      idstylist=%s",
               $this->db->GetSQLValueString($this->mysystem, "int"),
               $this->db->GetSQLValueString($this->branch, "int"),
               $this->db->GetSQLValueString($this->dateToday, "date"),
               $this->db->GetSQLValueString('1', "int"),
               $this->db->GetSQLValueString($staff, "int")
               );
    $result = $this->db->query($query); 
    $row = $this->db->fetch_assoc($result);
}