Find the total of the first two entries in a table using PHP SQL

Is there a way to calculate the total sum of the daily_rate_price field from only the first two rows in the daily_rates table?

daily_rate_id daily_rate_date daily_rate_price daily_rate_reservation
1480 2021-11-18 280.00 320
1481 2021-11-19 280.00 320
1482 2021-11-20 280.00 320

I attempted a query like this without success:

$accomTotal=" SELECT SUM(daily_rate_price) AS accomTotal FROM daily_rates   
WHERE daily_rate_reservation=$reservationId  LIMIT 2 ;";

Answer №1

To get the total accommodation price, you should consider using a subquery:

select sum(daily_rate_price) AS totalAccommodation 
from ( 
  select daily_rate_price from daily_rates 
  where daily_rate_reservation=$reservationId  
  order by daily_rate_date asc limit 2 
) t;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

utilizing switch case to handle login and registration of new users in a PHP application

I attempted to implement a switch case with login and new user submit button functionality, but I am only seeing the default case option with its corresponding values. Below is the code snippet: <?php switch ($_POST['submit']) { ...

Ensuring the Accuracy of Translated Objects within CakePHP 3

I'm currently facing some challenges while trying to validate an I18N field in CakePHP3. The setup for the translate behavior looks like this: $this->addBehavior('Translate', [ 'fields' => ['name', 'body ...

Having trouble sending JSON data to the server using a POST request

I am encountering an issue while attempting to send JSON data to the server using the fetch API and PHP as the server-side language. The PHP code on the server side is quite simple: <?php header("Access-Control-Allow-Origin: *"); header("Access ...

tips for modifying the URL to display the username directly from the URL

I am attempting to set up my profile page URL structure similar to Facebook's. For example, I want the URL to be example.com/johndoe which would display John Doe's profile page. Despite my efforts, I have not been able to accomplish this task suc ...

Uploading files using Ajax without the need for additional plugins or submitting a form

I have been struggling to find a solution for uploading files via Ajax when the input field is not enclosed within a form tag. I have already attempted this method. This is the HTML snippet: <span id="user-image-up-btn">Upload Image</span> &l ...

The third page of Reddit's JSON data on display

When utilizing the Reddit api, I have found a way to retrieve the json of a page. I have successfully done this for the front page, but now I am wondering how to obtain the json for the third page without navigating through the second page. Is there a met ...

I am experiencing difficulty in successfully transmitting a variable from my jQuery code to my PHP code

I've been attempting to pass a variable from my jQuery code to my HTML/PHP code using AJAX and POST. However, I'm encountering an error message stating "Notice: Undefined index: testData in C:\xampp\htdocs\teszt\test1.php on l ...

PHP's json_encode() compared to using an empty NSDictionary in iOS development

I have a question regarding how to handle JSON encoding/decoding in PHP without converting arrays or dictionaries. In my iOS app, I store data in an NSDictionary. Some of this data is nested and includes NSArrays or other NSDictionarys that may contain fu ...

I am looking to efficiently store and access a collection of form elements, where each group is represented as an array of form elements

I have created a form that consists of groups of elements stored in a database table. I populate the form with data from the table, allowing for edits to be made. Upon submission of the form, changes are saved back to the table based on the submitted form ...

Laravel's hasManyThrough relationship allows for linking to another model

I am currently facing a situation where the Parent and Student models are in a many-to-many relationship, each of them belonging to the User model. The Branch is connected to many Students through the User via hasManyThrough(Student::class, User::class);. ...

What is the easiest way to clear browser cache automatically?

Hello, I have implemented an ajax auto complete function in one of my forms. However, I am facing an issue where over time, the suggestions get stored and the browser's suggestion list appears instead of the ajax auto complete list, making it difficul ...

Laravel 8 - utilizing two foreign keys referencing the users table

I have an idea for an application that allows two people to create a meeting room, but I'm unsure of how to create the schema in Laravel. This is my attempt so far: Schema::create('appointments_tables', function (Blueprint $table) { ...

Retrieving the value of a PHP function using JavaScript

After reviewing various answers, I am still unsure of how to proceed with my issue. Here is the dilemma I'm facing: I am working with a .txt file to extract and interpret the meaning of a name (even though using a database would be more efficient). ...

Displaying a timer output in an HTML div every second using PHP

I created a custom PHP countdown timer that displays the remaining time in hours, minutes, and seconds (specifically 3 hours) - named countdown.php. The timer output is displayed within a <div> tag on another page called index.html. Now, I am lookin ...

Is PHP $_SESSION causing a hindrance in loading your website?

I have a basic webpage to check if sessions are properly set or not. session_start(); if (!isset($_SESSION['id'])) { $_SESSION['checkin'] = 'yes'; header("Location: https://blabla.php"); exit(); } else { // do ...

Arrange, Explore (Refine), Segment HTML Chart

Is there a way to efficiently sort, paginate, and search (based on a specific column) data in an HTML table? I've explored several JQuery plugins such as DataTable, TableSorter, Picnet Table Filter, but none seem to offer all three functionalities (se ...

Reorder the mySQL update statement to sort the table's order with an unlimited number of child elements, following the

I am faced with a table structure that looks like this: id | parent_id | order 1 | 0 | 1 2 | 1 | 1 3 | 1 | 2 4 | 1 | 3 5 | 0 | 2 6 | 0 | 3 7 | 5 | 1 8 | 5 | 2 Thi ...

Encountering a Laravel error at line 221 in HasOneOrMany.php while attempting to upload an image

I am currently working on a Laravel project and I am trying to implement a feature that allows users to post images. Unfortunately, I encountered the following error: FatalThrowableError in HasOneOrMany.php line 221: Type error: Argument 1 passed to ...

What is the best way to retrieve the previously chosen value from one dropdown and populate it into another dropdown when

I have 3 choices available. When the user selects the third option, a jQuery onchange event is triggered to send the variable to another PHP file for database validation based on the selected id. The challenge lies in how I can also include the variables f ...

Unable to output the string using htmlspecialchars

Oops! I made a mistake... just came across a JavaScript alert box! I just included htmlspecialchars. $src = './imgs/flag.jpg'; echo sprintf("<img alt=\"'.htmlspecialchars($name).'\" src=\"%s\" />", $src); I ...