Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

sql - How to select column from different tables based on condition in single query?

Is it possible to get the date column from the different table based on conditions in a single query?

  1. In SPE_common_data is the Main table, there is spe_content_type column has JOURNAL PAPER or CONFERENCE PAPER are values.

enter image description here

  1. If the column value is JOURNAL PAPER – use SPE_journal_data table and use spe_journal_pub_online_date as active date to publish paper enter image description here

  2. IF the column value is CONFERENCE PAPER – use SPE_conference_data table to find spe_meeting_code for that paper

enter image description here

4.And based on that spe_meeting_code go to Petro_meetings table and find early_start_date for that meeting code and use that date to publish that paper.

enter image description here

Is this possible for everything in a single query?

question from:https://stackoverflow.com/questions/66057935/how-to-select-column-from-different-tables-based-on-condition-in-single-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to union two queries by the set_content_type value, for example like this:

SELECT 
  cmn.spe_manuscript_num,
  cmn.spe_content_type,
  jrn.spe_journal_pub_online_date AS pub_online_date 
FROM spe_common_data cmn
JOIN spe_journal_data jrn ON cmn.spe_manuscript_num = jrn.spe_manuscript_num
WHERE cmn.spe_content_type = 'JOURNAL PAPER'
UNION ALL
SELECT 
  cmn.spe_manuscript_num,
  cmn.spe_content_type,
  pm.early_start_date AS pub_online_date 
FROM spe_common_data cmn
JOIN spe_conference_data cnf ON cmn.spe_manuscript_num = cnf.spe_manuscript_num
JOIN petro_meetings pm ON cnf.spe_meeting_code = pm.spe_meeting_code 
WHERE cmn.spe_content_type = 'CONFERENCE PAPER'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...