M
M
Matvey Slotvinsky2021-05-28 06:29:15
SQL
Matvey Slotvinsky, 2021-05-28 06:29:15

How to execute a SQL query on several identical tables and identify the data?

I am glad to welcome everyone, I ran into a problem of a non-standard nature.
Essence in the following:
There are 2 tables with the different data, but identical structure.

Example:
60b05f2eb0dd5482410094.png

What result should be obtained, and what conditions?
It is necessary to get all conditional "Grigorievs" using the minimum number of queries to the database, given that in the table I received, I must somehow distinguish from which table this person was received: a nurse or a doctor.
In addition to all this, it is necessary to take into account that we will display the data page by page. Those. the data will not be displayed all, but in the form of pagination, which adds to the task the need to obtain a preliminary number of records that meet our condition (Number of Grigorievs).

You cannot change the structure of the database, at the moment each table has + - 3 million rows, respectively, hence the need to use limits and achieve a minimum number of database accesses.

What was I trying to do?

At first I tried to get a table by combining the results using UNION , the number of preliminary results was counted using SQL_CALC_FOUND_ROWS , I get everything, but I can not identify the data after receiving them. After all, the table structures are the same.
Then I tried to join tables using JOIN , I did not achieve anything special. Maybe I go somewhere nearby, but I can not get to the point.

Technologies
10.1.48-MariaDB
Queries are executed from a node js script using mysql2

Maybe I'm just a noob, any help would be appreciated

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-05-28
@Zak0

SELECT 1 AS `table_id`, `id`, `name`, ...
  FROM `table1`
  WHERE ...
UNION SELECT 2, `id`, `name`, ...
  FROM `table2`
  WHERE ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question