我有学生和培训师表:
学生表:
student_id (primary key)
name
email
培训师表:
trainer_id
student_id
amount
输出必须:
sid name email amount
22 ram r@g 200
34 sam r@f
我想从学生表中获取(student_id,姓名,电子邮件)和从培训师表中获取(金额)(imp:trainer_id和student_id应匹配(如sid = 46,tid = 78,金额= 500)然后只有金额必须显示值。否则金额将显示为空,但(student_id,姓名,电子邮件)应显示)
在培训师表格中,student_id和trainer_id必须匹配...基于该金额将来......我的意思是,如果我们将选择查询发送为“从培训师中选择金额,其中student_id = 20且trainer_id = 36 ......” 。该列应匹配sid和tid
答案 0 :(得分:0)
如果您这样做,如果金额为空,将不会显示数据:
select st.student_id,
st.name,
st.email,
tt.amount
from student_table st, trainer_table tt
where st.student_id = tt.student_id
NVL函数允许您在遇到空值时替换值, 所以如果你这样做,它将显示数据并显示0而不是null:
select st.student_id,
st.name,
st.email,
(nvl(select tt.amount
from trainer_table tt
where st.student_id = tt.student_id,0))) amount
from student_table st
答案 1 :(得分:0)
这可以通过PLSQL实现。对不起,它是如此广泛,但我希望它能让你看到PLSQL的强大功能,如果你需要根据条件操纵数据。
DECLARE
TYPE result_record IS RECORD
( sid NUMBER
, name VARCHAR2(60)
, email VARCHAR2(60)
, amount NUMBER);
CURSOR c IS select st.student_id sid,
st.name name,
st.email email,
tt.amount amount
from student_table st, trainer_table tt
where st.student_id = tt.student_id;
TYPE results_table IS TABLE OF results_record INDEX BY BINARY_INTEGER;
c_rec c%ROWTYPE;
temp_rec RESULTS_RECORD;
results RESULTS_TABLE;
lv_index NUMBER := 0;
BEGIN
OPEN c;
WHILE lv_index <= c%ROWCOUNT LOOP
FETCH c INTO c_rec;
temp_rec.sid := c_rec.sid;
temp_rec.name := c_rec.name;
temp_rec.email := c_rec.email;
temp_rec.amount := c_rec.amount;
results(lv_index) := temp_rec;
lv_index := lv_index + 1;
END LOOP;
CLOSE c;
-- Now we can access and modify our table from inside PLSQL
SELECT * FROM results;
-- Use PLSQL logic to make the table output pretty with '$' and conditionals
FOR i IN results LOOP
dbms_output.put_line(i.sid||' $'||i.amount); -- example for how to access
-- your code here
END LOOP;
END;
/
一如既往,我希望这会给你一些想法。
-V