加入3个表并从第3个表中获取最新记录(如果不存在)显示最新的第2个表 [英] Join 3 tables and fetch for latest record from 3rd table if not exists display latest from 2nd table
本文介绍了加入3个表并从第3个表中获取最新记录(如果不存在)显示最新的第2个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请在这里找到小提琴
我的要求是我需要根据第一个表ID和第二个表ID显示第3个表中的最新记录,如果不是我想显示最新的从第二张表记录。我已经给出了样本表和我写的查询,但是根据我的需要得不到实际结果所以有人可以帮助我
SQL小提琴
这就是我得到的东西,我写的是我的预期
http://i.stack.imgur.com/KCFkA.png [ ^ ]
Please find the fiddle here
My requirement is I need to display the latest record from 3rd table if exists based on first table ID and second table ID, if not I would like to display the latest record from second table. I have given the sample tables and the query I have written but i am not getting the actual result as per my need so some one can help me
SQL Fiddle
This is what I am getting and I have written what I expected
http://i.stack.imgur.com/KCFkA.png[^]
推荐答案
我无法从工作中访问图像URL。但我想我可能已经理解了你需要的东西。您可以在查询中添加更多列。
I can't access the image URL from work. But I guess I may have understood what you need. You can add more columns to the query.
SELECT ISNULL(CDESC,BDESC)
FROM TBLA JOIN TBLB
ON TBLA.AID = TBLB.AID
LEFT JOIN TBLC
ON (TBLB.BID = TBLC.BID AND TBLB.AID = TBLC.AID)
ORDER BY TBLC.CID DESC,TBLB.BID DESC
这篇关于加入3个表并从第3个表中获取最新记录(如果不存在)显示最新的第2个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文