M
M
mega epic2019-04-05 12:46:48
SQL
mega epic, 2019-04-05 12:46:48

What is the correct way to write SELECT with MAX()?

Hello. I have a table 5ca693f1f01f4227678715.jpeg
One "site" can have several "receipts". I am writing a query in which I want to get the record with the highest receipt number for a particular parcel. I am using mysql driver for nodeJS

case('getReceiptNumber'):
  debug.ok('Запрос номера квитанции');
  var values = [
    receipt',
    'number',
    'number',
    'receipt',
    'rpu',
    frontReq.data //тут номер участка, например 100
  ];

  databaseReal("SELECT * FROM ?? WHERE ?? = (SELECT MAX(??) FROM ??) AND ?? = ?", values, function(req){
      ajaxResponse = req;
      res.end(JSON.stringify(ajaxResponse));
      debug.ok(req);
    });
break;

those. in this query, only the parcel number changes dynamically.
The problem is that when I submit a request with a section number of "100", a line with a receipt number of 9 is returned to me, although at the time of the request there is already an entry with a receipt number of 10 (see the first plate).
And when I submit the section number 101, there is no result at all, although there are lines.
Maybe a curve request?
ps databaseReal() - my wrapper for the mysql.query method and working with the connection, everything is ok there

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mega-epic, 2019-04-05
@mega epic

Thank you all, thanks to you made:

SELECT * FROM 'таблица' 
WHERE 'номер участка' = '100' 
ORDER BY 'номер квитанции' DESC 
LIMIT 1

I'm going to learn sql

R
Rsa97, 2019-04-05
@Rsa97

SELECT `Номер участка`, COUNT(*) AS `count`
  FROM `table`
  ORDER BY `count` DESC
  LIMIT 1

I
irishmann, 2019-04-05
@irishmann

Created a table
Wrote a query
Result
5ca72c745fc0f736820981.jpeg

A
Andrey, 2019-04-05
@VladimirAndreev

select b.*
(select max(nom_kvit) as nom_kvit from data_tale where nom_uch=1) as a
inner join data_table b using(nom_uch, nom_kvit)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question