简体   繁体   English

如何根据一个表中的数据选择一个表中的数据

[英]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_listContracts_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 Con​​tracts_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.

相关问题 MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? MYSQL:如何根据另一个表中的数据选择列? - MYSQL: How do select column depending on data from another table? 我可以从一个表中随机选择一行并使用静态标识符来选择另一张表中的相应数据吗? - Can I randomly select a row from one table and use a static identifier to select corresponding data in another table? 从一个表中选择数据,取决于另一表的相似度 - select data from one table depending smilarity of other table 在第一个表中插入数据后,如何将数据从一个表插入到另一个表? - How can I insert data from one table to another table after insert a data in first table? Select语句,该语句根据一个表中的数据计算一个表中不同条目的总数 - Select statement that that counts total number of distinct entries in one table, depending on data from another table 如何将图像数据从一张桌子移动到另一张桌子? - How can I move image data from one table to another? 如何将数据从一个表转移到另一个表? - How can I transfer data from one table to another? 如何在MySQL中使用SELECT语句从一个表中获取数据,并从另一表中覆盖数据? - How do I take data from one table, and overwrite it from another table, with a SELECT statement in MySQL? 根据同一表中的数据选择数据 - Select data depending on data from the same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM