Is it possible to choose from two tables of the database using one query and sort them in alternation, that is, in the final selection the records alternate (2 out of 1.2 out of -2 and so on)?

  • specify the sql dialect, and you need two from the first, two from the second, and so on? or what? in any case, you need a union for the tables, and a numbering of the lines in them. then sorting - teran
  • How to choose from two tables, I know, I still need to organize the selected records by interleaving also through the query. - Oleg Bass
  • if the table structures are the same - use (SELECT * FROM table1) UNION (SELECT * FROM table2) with the necessary LIMIT order - sort? - sterx
  • I need to choose the data already taking into account the alternation - Oleg Bas
  • one
    For example, you can take the line number starting at 0 and reset the low bit ( & 0xFFFFFFFE ) (or subtracting the remainder of the division by 2). We get the numbers 0, 0, 2, 2, 4, 4, ... From the second table, the same but +1. It remains to make the union and sort by this field. But how exactly to get line numbers depends on the sql dialect. up to version 8 in MySQL will have to use variables In version 8 of MySQL and in the overwhelming majority of other DBMS, you can use the row_number () function - Mike

0