Skip to content
Advertisement

Get original and current prices with a subquery in MySQL with PHP

I have a table of ads and another of prices, relative to these ads. When the user modifies a price in the application, a new row is added to the prices table, which includes the price and the date the price was modified.

I need to get a list with all the ads and, for each ad, the first price that was registered, and also the last. I have used a double subquery on the same price table.

Tables

ads

id int ad_title varchar
1 Potatoes
2 Tomatoes

prices

id int price decimal price_timestamp timestamp ads_id int
1 50 2021-02-16 21:12:36 1
2 5 2021-02-17 21:12:48 1
3 1000 2021-02-17 21:20:40 2
4 900 2021-02-18 13:20:49 2
5 700 2021-02-18 13:20:49 2

Query

JavaScript

Esta consulta devuelve lo siguiente en mi servidor local (XAMPP):

ad_title price_first price_current
Potatoes 50 5
Tomatoes 1000 700

As you can see the result of the query is correct BUT when it is executed on a server from an external provider (I have tested it in 1&1 IONOS and in Arsys Spain) the results vary practically with each execution. There are times when prices appear in reverse order, sometimes the same price appears in both columns…

What I need?

I need to understand if the problem is in the servers configuration of these providers or if the query is wrong.

I am also open to ideas that you can contribute to get the prices (first and current) in another way, even if it is with another structure in the database.

Advertisement

Answer

You could also try using a subquery for min and max date

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement