如何正确排序结果以按以下顺序显示结果
1st - Parent Name
2nd - Child Name
3rd - Teacher Name
4th - Other Name
父名称和子名称在同一个表Family
上id | parent name | child name
-----------------------------
1 Denz Hanz
2 Denz Pog
3 Joann Mac
而其他名称在不同的教师表上
id | teacher name
-----------------
1 Miguel
2 Sean
和Other_guest表
id | guest name
-----------------
1 Mike
2 Mal
如果父母没有到达,则会显示子名称。查询结果应该显示类似这样的内容
Participant Name
----------------
1. Denz
2. Denz
3. Mac
4. Miguel
5. Sean
6. Mal
7. Mike
我尝试使用order by field(),按字段asc,field2 dec ...等排序,但似乎不是我们想要的结果。
答案 0 :(得分:0)
以下查询适用于大多数数据库,此查询将首先按类型排序,然后命名:
SELECT CustomerID,CustomerName,OrderValue
FROM Customers
WHERE CustomerID IN (SELECT CustomerID
FROM Orders
WHERE OrderValue > 10000
)
答案 1 :(得分:0)
以下是我对Oracle DB的解决方案:
请注意,UNION在后续select语句中不允许使用ORDER BY子句,因此需要一个过程来生成GUESTS表中的guest虚拟机列表。
表ARRIVED包含到达父项的ID,所有其他表都是自解释的。
create table Family (id number, parentname varchar2(100), childname varchar2(100));
create table Teacher (id number, teachername varchar2(100));
create table Other_guest (id number, guestname varchar2(100));
create table Arrived (id number);
create table Guests (guestname varchar2(100));
create sequence Family_seq start with 1 order;
create sequence Teacher_seq start with 1 order;
create sequence Other_seq start with 1 order;
create or replace trigger Family_id before insert on Family for each row
begin
select Family_seq.nextval into :new.id from dual;
end;
create or replace trigger Teacher_id before insert on Teacher for each row
begin
select Teacher_seq.nextval into :new.id from dual;
end;
create or replace trigger Other_id before insert on Other_guest for each row
begin
select Other_seq.nextval into :new.id from dual;
end;
insert into Family (parentname, childname) values ('Denz', 'Hanz');
insert into Family (parentname, childname) values ('Denz', 'Pog');
insert into Family (parentname, childname) values ('Joann', 'Mac');
insert into Teacher (teachername) values ('Miguel');
insert into Teacher (teachername) values ('Sean');
insert into Other_guest (guestname) values ('Mike');
insert into Other_guest (guestname) values ('Mal');
insert into Arrived (id) values (1);
insert into Arrived (id) values (2);
create or replace procedure update_guest_list as
pragma autonomous_transaction;
cursor c_parents is select parentname from family where id in (select id from arrived) order by family.parentname;
cursor c_children is select childname from family where id not in (select id from arrived) order by family.childname;
cursor c_teachers is select teachername from teacher order by teacher.teachername asc;
cursor c_others is select guestname from other_guest order by other_guest.guestname asc;
begin
delete from guests;
for parents_rec in c_parents
loop
insert into guests (guestname) values (parents_rec.parentname);
end loop;
for children_rec in c_children
loop
insert into guests (guestname) values (children_rec.childname);
end loop;
for teachers_rec in c_teachers
loop
insert into guests (guestname) values (teachers_rec.teachername);
end loop;
for others_rec in c_others
loop
insert into guests (guestname) values (others_rec.guestname);
end loop;
commit;
end;
/
execute update_guest_list;