db_select get value of the field I have order form with field reference number. when a new order is submitted i need to get the last reference number. I did something similar long time ago in Drupal 7 but now i need to do it in Drupal 8 and i don't know how. I noticed that fields in database are written as 'node__field_ref_number' 

DRUPAL 7
$query = db_select('field_data_field_order_reference_number', 'f');
$query-&gt;condition('f.bundle', 'order', '=');
$query-&gt;fields('f', array('field_order_reference_number_value'));
$query-&gt;orderby('field_order_reference_number_value','DESC');
$query-&gt;range(0, 1);
$query-&gt;execute();
$result = $query-&gt;execute();
$record = $result-&gt;fetchAssoc();
//drupal_set_message('previous order №: ' .print_r($record['field_order_reference_number_value'], true));
$title =($record['field_order_reference_number_value']) + 1;
//drupal_set_message('order №: ' .print_r($title, true));
$form_state['node']-&gt;field_order_reference_number['und'][0]['value'] = $title;
$form_state['node']-&gt;title= 'reference number ' .$title;


So far i can get only nid of nods of that type (content_type_order)

DRUPAL 8
$query = \Drupal::database()-&gt;select('node', 'n');
$query-&gt;addField('n', 'nid');
$query-&gt;condition('n.type', 'content_type_order');
$results = $query-&gt;execute()-&gt;fetchAll();
drupal_set_message('value' .print_r($results, true));
If I understood correctly, this snippet would get you the number:

use Drupal\Core\Database\Database;

$query = \Drupal::database()-&gt;select('node__field_order_reference_number', 'f');
$query-&gt;fields('f', ['field_order_reference_number_value']);
$query-&gt;orderBy("revision_id", 'DESC');
$query-&gt;range(0, 1);
$last_order_reference_number = $query-&gt;execute()-&gt;fetchCol()[0];


If you need to further understand the database schema related to your field, you could try these commands:

mysql> show tables like '%order_reference_number%';

+----------------------------------------------+
| Tables_in_drupal8 (%order_reference_number%) |
+----------------------------------------------+
| node__field_order_reference_number           |
| node_revision__field_order_reference_number  |
+----------------------------------------------+
2 rows in set (0.00 sec)


mysql> describe node__field_order_reference_number;

+------------------------------------+------------------+------+-----+---------+-------+
| Field                              | Type             | Null | Key | Default | Extra |
+------------------------------------+------------------+------+-----+---------+-------+
| bundle                             | varchar(128)     | NO   | MUL |         |       |
| deleted                            | tinyint(4)       | NO   | PRI | 0       |       |
| entity_id                          | int(10) unsigned | NO   | PRI | NULL    |       |
| revision_id                        | int(10) unsigned | NO   | MUL | NULL    |       |
| langcode                           | varchar(32)      | NO   | PRI |         |       |
| delta                              | int(10) unsigned | NO   | PRI | NULL    |       |
| field_order_reference_number_value | varchar(255)     | NO   |     | NULL    |       |
+------------------------------------+------------------+------+-----+---------+-------+
7 rows in set (0.23 sec)


mysql> select * from  node__field_order_reference_number;

+--------+---------+-----------+-------------+----------+-------+------------------------------------+
| bundle | deleted | entity_id | revision_id | langcode | delta | field_order_reference_number_value |
+--------+---------+-----------+-------------+----------+-------+------------------------------------+
| page   |       0 |        17 |          28 | en       |     0 | 123                                |
+--------+---------+-----------+-------------+----------+-------+------------------------------------+
1 row in set (0.00 sec)