[英]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可以说我正在获取这些数据
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.