简体   繁体   English

SQL 查询用于查找具有连接的数据或多个表中的同一表中的位置

[英]SQL query for Finding data with join or where within same table from multi tables

Im sorry i cant explain the question对不起,我无法解释这个问题

I have a problem that i need to join 4 tables together to find some information.我有一个问题,我需要将 4 个表连接在一起以查找一些信息。 Im using inner join which is bringing me the right data but duplicate row having a minor change data.我正在使用内部联接,它为我带来了正确的数据,但重复的行具有微小的变化数据。 Lets say im getting this data可以说我正在获取这些数据

See the Table here请参阅此处的表格

If we look at the results.如果我们看看结果。 At the last 4 rows we can notice simillar results.在最后 4 行,我们可以注意到类似的结果。 What i want to achieve is these 4 rows converted into 2.我想要实现的是将这 4 行转换为 2 行。

I only want rows whose account id = vehicle account id and 110. I want these two rows.我只想要帐户 id = 车辆帐户 id 和 110 的行。我想要这两行。 The join query i write is as follows我写的join查询如下

 select Distinct vh.VehicleNo,jd.AccountID, tr.ID,jd.Memo,je.Description, jd.Cr,jd.Dr ,jd.Detail, je.JEntryId,      je.ExpenseID from tbl_JDetail jd
 inner join tbl_JEntry je on je.JEntryId = jd.JEntryID
 inner join tbl_Trip tr on tr.ID = je.RefID
 inner join tbl_Vehciles vh on vh.VID = tr.VehicleID

I really need to achieve this.. Any help would be appreciated how can i achieve.我真的需要实现这一点。我将如何实现任何帮助将不胜感激。 What i want is let me write the query in simple langauge我想要的是让我用简单的语言编写查询

The query should first find rows in jdetail with accountid = 110. Then get its jentryid.查询应首先在 jdetail 中找到 accountid = 110 的行。然后获取其 jentryid。 Then in Jentry it should find the tripid and in trip table i have the vehicle id.然后在 Jentry 中它应该找到 triid 并且在行程表中我有车辆 ID。 Vehicle table contains vehicle id.车辆表包含车辆 ID。

Then the query should find in jdetail with same jentryid and vehicleaccountid然后查询应该在 jdetail 中找到相同的 jentryid 和 vehicleaccountid

i need jdetail accountid = 110 and accountid = vehicle.accountid我需要 jdetail accountid = 110 和 accountid = vehicle.accountid

The vehicle relation is Jdetail > Jentry > Trip > Vehicle车辆关系为 Jdetail > Jentry > Trip > Vehicle

i hope i was clear enough我希望我足够清楚

Please add supporting information (create table statements, sample data, current output and desired output) to your question.请在您的问题中添加支持信息(创建表语句、示例数据、当前 output 和所需输出)。 As you have not included supporting information, I am unable to test this.由于您没有提供支持信息,因此我无法对此进行测试。 This is my "best guess" based on your description.根据您的描述,这是我的“最佳猜测”。

select Distinct vh.VehicleNo,jd.AccountID, tr.ID,jd.Memo,je.Description, jd.Cr,jd.Dr ,jd.Detail, je.JEntryId,      je.ExpenseID
from tbl_JDetail jd
inner join tbl_JEntry je
    on je.JEntryId = jd.JEntryID
inner join tbl_Trip tr
    on tr.ID = je.RefID
inner join tbl_Vehciles vh
    on vh.VID = tr.VehicleID
    and vh.AccountID = jd.AccountID
where jd.AccountID = 110

Solved it by using 2 CTEs.通过使用 2 个 CTE 解决了它。 CTE1 finds the jentry_id of all records using account ID = 110 CTE1 使用 account ID = 110 查找所有记录的 jentry_id

and CTE2 find all the required data with inner join cte on cte.JEntryId = jd.JEntryID和 CTE2 在 cte.JEntryId = jd.JEntryID 上使用内连接 cte 找到所有需要的数据

It solved all my problems..它解决了我所有的问题..

Thankyou.谢谢你。 Hope it help someone else too希望它也能帮助别人

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于查找每个组中成员相同的记录的 SQL 查询 - SQL query for finding records where members within each group are the same SQL查询联接/哪里3表 - Sql query Join/Where 3 Tables SQL查询-如何联接/联合3个表(2个索引表和1个数据表) - sql query - how to join/union 3 tables (2 index tables and 1 data table) SQL SELECT WHERE NOT IN来自同一个表查询 - SQL SELECT WHERE NOT IN from same table query SQL查询以显示表A中的数据和表B中的数据,其中使用联接在表B中找到表A的parent_id - Sql query to display data from table A and data from table B where parent_id of table A is found in table B using join 如何通过条件查询(左联接/右联接/内部联接/多选择,…或)从多表中获取数据 - How to fetch data by a conditional query(left join/right join/inner join/ multi select, … or) from multi table 如果我们从第二个表的同一列中的行中分离数据,SQL查询如何联接2个表 - How sql query to join 2 table if we separated data from rows in same column on second table 带count()联接的SQL查询以及对表的查询 - Sql query with count() join and where on to tables 有没有办法将 SQL 中的查询中的某些数据批量插入到同一个表中具有相同行的另一列? - Is there a way to bulk insert certain data from a query in SQL to another column with the same row within the same table? 查询显示两个表中的0条记录在联接上具有相同的数据 - query show 0 record from two tables have same data on join
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM