S
S
Ska1n2015-03-12 00:09:36
Database
Ska1n, 2015-03-12 00:09:36

What is the best way to implement the mechanism of "weight" of values ​​in a database table?

Greetings, I titled the subject a little chaotically, I will explain - there are, say, records in the database:
PK name
1 test1
2 test2
5 test3
, etc.
In normal mode, I can display records from the table, sorting them by the main key, or name, but how best to implement such scheme, if I want to, say, first display the value with pk = 5, after already with 1 and 2? It only comes to mind to create a separate table with a 1 to 1 ratio, where the weight of each record will be stored, and every time I want to swap the records, overwrite these same weight keys ... Am I thinking in the right direction, or are there more universal ones for a similar purpose? solutions?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
George, 2015-03-12
@Ska1n

To output pk=5 and then 1, 2, try to make a query like:
You didn't specify which database is used, the syntax of the query may differ slightly on different systems, but as a rule, the database allows such calculations in the sort expression.

V
Vyacheslav Uspensky, 2015-03-12
@Kwisatz

Write a real example of where and why you need it, without let's say.

A
Anton Spirin, 2018-01-03
@IvanIvanIvanIvanIvan

Here is a generic and extensible variant:

var auto = [
  {
    marka: "BMW",
    model: "4k",
    cuzov: "A",
 },
 {
   marka: "Merc",
   model: "4k",
   cuzov: "B",
 },
 {
   marka: "Audi",
   model: "3k",
   cuzov: "C",
 },
 {
    marka: "Toyota",
    model: "1k",
    cuzov: "D",
 },
 {
    marka: "Lexus",
    model: "2k",
    cuzov: "E",
 },
 {
    marka: "Rang",
    model: "3k",
    cuzov: "F",
 },
 {
   marka: "BMW",
   model: "4k",
   cuzov: "G",
 },
 {
   marka: "BMW",
   model: "2k",
   cuzov: "Y",
 },
 {
   marka: "Audi",
   model: "3k",
   cuzov: "U",
 }
];

var marka = ["Audi", "Rang", "Merc"];
var model = ["4k", "2k", "3k"];
var cuzov = [];
var params = { marka, model, cuzov };
var activeKeys = Object.keys(params).filter(key => params[key].length);   
  
var rez1 = auto.filter(x => activeKeys.every(key => params[key].includes(x[key])) );

console.log(rez1);

You can extend the filtering options object. Parameters without elements are filtered out to the comparison stage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question