Ask for help with sql?

Table 1: appl_id, name, sex, id_No
Table 2: appl_id, appl_date, busin_key

in the following time period, find out the nearest time (appl_date) (appl_id) according to (id_No) (duplicate data in Table 1, appl_id is the associated primary key)

to_date ("2018-05-23 21mm to_date HH24:MI:SS")
to_date

Mar.17,2021
The meaning of the

title is a little unclear, but the SQL is relatively simple.
Let me try
select * from Table 1
where appl_id in (select appl_id from Table 2 where appl_time between 'start time' and 'end time')
and id_no =? (represents parameter)


time is closest, that is, it is sorted in descending order of time, from big to small, that is, ORDER BY appl_time DESC
also limits the time range,

it is better to have desensitized data samples and database types, such as mysql or sql server

just want to extract id:
SELECT appl_id FROM Table 2 WHERE appl_time BETWEEN '2018-05-23 21 SELECT appl_id FROM 0015 00' AND' '2018-05-29 23 23 15 59 SELECT appl_id FROM 59' ORDER BY appl_time DESC

extract information:
SELECT Table 1. Appl _ id, Table 1. Name, Table 1. Sex FROM Table 1
LEFT JOIN Table 2 ON Table 2. Appl _ id = Table 1. Appl _ id
WHERE Table 2. Appl _ time BETWEEN '2018-05-23 21 WHERE 00' AND' 2018-05-29 23 23 FROM 59 'ORDER BY Table 2. Appl _ time DESC

how to repeatedly process the extracted information, you need to provide data samples and specific database types


since you did not specify which two fields of the two tables are associated, I will default to the appl_id of Table 1 and the appl_id association of Table 2

.
SELECT a.appl_id,MAX(b.appl_date) FROM [Table_1] a 

INNER JOIN [Table_2] b ON a.appl_id=b.appl_id

WHERE b.appl_date BETWEEN '2018-05-23 21:00:00','yyyy-mm-dd HH24:MI:SS' AND '2018-05-29 23:59:59','yyyy-mm-dd HH24:MI:SS'

AND a.id_No=xxxxxx id_No

GROUP BY a.appl_id

give you the idea, and the code will not be written. First, since it is an associated table, then first use left join to splice a view, and then according to the time you set for the where condition, and finally, because you want to get the most recent time, so sort the time, and finally, because your data may be duplicated, so sort the time followed by the primary key.

Menu