2011-07-01 17 views
4

選擇字段,我需要選擇這些領域-----如何從兩個表

user_id, 
sales_vertical, 
partner_id, 
category, 
sub_category, 
stage_id, 
exp_revenue, 
action, 
action_date, 
title_action, 
date_action, 
details from opportunity_history table 

tri_title, 
tri_subtitle 
from res_partner table 
where res_partner.partner_id = opportunity_history.partner_id 
在單個查詢

。 我們該怎麼做?

感謝阿迪爾

+0

你可以試試更好地設置你的問題的格式,並提供更多的背景。問題的當前狀態使我們難以真正看到你想要做什麼。 –

+0

可能的重複[Simple SQL Select from 2 Tables(什麼是Join?)](http://stackoverflow.com/questions/11040587/simple-sql-select-from-2-tables-what-isa-a-加入) – FoamyGuy

回答

1
SELECT a.user_id, a.sales_vertical, a.partner_id, a.category, a.sub_category, 
a.stage_id, a.exp_revenue, a.action, a.action_date, a.title_action, a.date_action, 
a.details, b.tri_title, b.tri_subtitle FROM opportunity_history a, res_partner b 
WHERE b.partner_id =a.partner_id 
3
select user_id, sales_vertical, partner_id, category, sub_category, stage_id, exp_revenue, action, action_date,title_action,date_action,details, tri_title, tri_subtitle 

from opportunity_history, res_partner 

where res_partner.partner_id =opportunity_history.partner_id 
11

爲什麼有這麼多downvotes並沒有意見?

嘗試:

SELECT 
    oh.ser_id, 
    oh.sales_vertical, 
    oh.partner_id, 
    oh.category, 
    oh.sub_category, 
    oh.stage_id, 
    oh.exp_revenue, 
    oh.action, 
    oh.action_date, 
    oh.title_action, 
    oh.date_action, 
    oh.details, 

    rp.tri_title, 
    rp.tri_subtitle 

FROM opportunity_history AS oh 
    INNER JOIN res_partner AS rp 
    ON rp.partner_id = oh.partner_id 
+0

這麼多downvotes和沒有評論,因爲很明顯,有人甚至沒有試圖找出答案。在一些「7天內SQL」中,聯接就像第2天一樣。 (剛剛檢查過...他們在21天內的「第6天」[Literally。](http://web.ing.puc.cl/~jlortiz/Teach_Yourself_SQL_in_21_days_FULL.pdf)) – cHao

+3

但是,他們應該告訴他 - 我也不喜歡懶惰的人,但我認爲他們可能會被幫助得到糾正:) –

1

可能的解決方法(而不是單個查詢)

(SELECT user_id, sales_vertical, partner_id, category, sub_category, stage_id, exp_revenue, action_date, title_action, date_action, details FROM opportunity_history) UNION ALL (SELECT tri_title, tri_subtitle FROM res_partner WHERE res_partner.partner_id=opportunity_history.partner_id)