[英]How can I select data from one table depending on the data from another table
I have 2 tables: contracts_main_list and contracts_detail . 我有2个表: Contracts_main_list和Contracts_detail 。 In contracts_main_list I have columns: 在Contracts_main_list中,我有几列:
user_id
contract_id
and in contracts_detail: 并在contract_detail中:
contract_id
other columns with data
I need to select all the rows from the table contracts_main_list WHERE user_id = some number
. 我需要从表contract_main_list中选择所有行,其中WHERE user_id = some number
。 From these rows I need to get the list of contract numbers (from column contract_id
) and according to them select rows corresponding to each of the contract number from the list. 从这些行中,我需要获取合同编号列表(来自contract_id
列),并根据它们从列表中选择与每个合同编号相对应的行。 So something like: 所以像这样:
WHERE contracts_detail.contract_id = contracts_main_list.contract_id
The contract_ids are probably gonna be unique, but in case there is some kind of error and there will be more rows with the same contract_id in either of the tables, I need to select only one row (so probably using DISTINCT) and select the latest record (both tables have a column id as a primary key) contract_ids可能是唯一的,但是如果出现某种错误,并且在任何一个表中会有更多具有相同contract_id的行,我只需要选择一行(因此可能使用DISTINCT)并选择最新的记录(两个表都有一个列ID作为主键)
Also if there is no row in contracts_detail matching with the contract_id to the contract_id of the first table contracts_main_list
it should skip the row. 同样,如果在contract_detail中没有行与第一张表contracts_main_list
contract_id匹配的contract_id,则应跳过该行。 But I guess the condition: 但是我想条件是:
WHERE contracts_detail.contract_id = contracts_main_list.contract_id WHERE Contracts_detail.contract_id = Contracts_main_list.contract_id
already covers it. 已经涵盖了。
I hope I made it clear enough. 我希望我说得足够清楚。 What I am trying to do in real life is show list of contracts with all the relevant data belonging to the user. 我在现实生活中试图做的是显示带有用户所有相关数据的合同清单。
To sum this up, I need to find all the contracts belonging to the user and select the rows with details about each contract and finally get the data from the contracts_detail table as a result. 综上所述,我需要找到所有属于用户的合同,并选择包含有关每个合同的详细信息的行,并最终从Contracts_detail表中获取数据。
Here is the result you're looking for: 这是您要寻找的结果:
SELECT CD.*
FROM (SELECT C2.contract_id
,MAX(C2.id) AS last_main_list_id
,MAX(CD2.id) AS last_contracts_detail_id
FROM contracts_main_list C2
INNER JOIN contracts_detail CD2 ON CD2.contract_id = C2.contract_id
GROUP BY C2.contract_id) L
INNER JOIN contracts_main_list C ON C.id = L.last_main_list_id
AND C.user_id = ?
INNER JOIN contracts_detail CD ON CD.id= L.last_contracts_detail_id
This query use a subquery for the FROM
because of the following indication you provided: 由于您提供了以下指示,因此该查询对FROM
使用子查询:
The contract_ids are probably gonna be unique, but in case there is some kind of error and there will be more rows with the same contract_id in either of the tables, I need to select only one row contract_ids可能是唯一的,但是如果出现某种错误,并且在任何一个表中会有更多具有相同contract_id的行,我只需要选择一行
If you're sure that the contract_id
are unique, here is the same query without this check on contract_id
: 如果您确定contract_id
是唯一的,则这是没有对contract_id
检查的相同查询:
SELECT CD.*
FROM contracts_main_list C
INNER JOIN contracts_detail CD ON CD.contract_id = C.contract_id
WHERE C.user_id = ?
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.