DWQA QuestionsCategory: DatabaseAsk for help to answer SQL?
biron asked 4 weeks ago

Table 1: Apple_ id、name、sex、id_ No
Table 2: Apple_ id、appl_ date、busin_ Key
In the following time period, according to (ID_ No) find the time (appl_ Date) latest (Apple)_ ID) (data in Table 1 is duplicate, appl_ ID is the associated primary key)
to_date(‘2018-05-23 21:00:00′,’yyyy-mm-dd HH24:MI:SS’)
to_date(‘2018-05-29 23:59:59′,’yyyy-mm-dd HH24:MI:SS’)

4 Answers
East wind annoys me answered 4 weeks ago

The meaning of the question is a little unclear, but this SQL is relatively simple.
I’ll 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 = ? (indicates the parameter)

Hank answered 4 weeks ago

The most recent time is sorted in descending order of time, i.e. order by apple_ time DESC
At the same time, it 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:00:00′ AND ”2018-05-29 23:59: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 II. Appl_ Time between ‘2018-05-23 21:00:00’ and ‘2018-05-29 23:59:59’ order by Table 2_ time DESC
How to repeatedly process the extracted information needs to provide data samples and specific database types

Unique answered 4 weeks ago

Since you did not specify which two fields are associated with the two tables, I will default to table 1’s appl_ ID and appl of Table 2_ ID Association

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 enter the ID you want to query here_ No

GROUP BY a.appl_id
Mr. Chen er answered 4 weeks ago

I’ll give you some ideas. I won’t write the code. 1、 Since it is an associated table, first use the left join to splice a view, and then set the time as the where condition according to the time you set. Finally, because you want to get the latest time, you sort the time. Finally, because your data may be repeated, the primary key sorting is added to the sorting time.