0

我们cardID在查询后返回了一个 's 列表,这些cardID's 属于两个表StudentPersonnel。那么我怎样才能加入那些' ' cardID''''''' ' ''''''''''''''''''''StudentPersonnelStudentPersonnelcardID

人员表:

PERSONNELID NUMBER(9,0) 
PERSONNELNAME   VARCHAR2(20)    
PERSONNELSURNAME    VARCHAR2(20)    
PERSONNELJOB    VARCHAR2(40)    
PERSONNELCARDID NUMBER(4,0) 

学生表:

STUDENTID   NUMBER(9,0) 
STUDENTNAME VARCHAR2(20)    
STUDENTSURNAME  VARCHAR2(20)    
STUDENTDEPT VARCHAR2(40)    
STUDENTFACULTY  VARCHAR2(20)    
STUDENTCARDID   NUMBER(4,0) 

卡ID

   CARDID   NUMBER(4,0) 
   USERTYPE VARCHAR2(20)    
   CHARGE   NUMBER(3,2) 
   CREDIT   NUMBER(4,2) 

支付设备表:

    ORDERNO NUMBER  
    PAYDEVIP    NUMBER(8,0) 
    PAYDEVDATE  DATE    No  
    PAYDEVTIME  VARCHAR2(8) 
    CHARGEDCARDID   NUMBER(9,0) 
    MEALTYPE       VARCHAR2(10)

我试图返回 2012 年 12 月 27 日在自助餐厅用餐的前 10 人的姓名和姓氏

SELECT C.CARDID
FROM CARD C, PAYMENTDEVICE P
WHERE P.ORDERNO
BETWEEN (SELECT MIN(ORDERNO)
FROM PAYMENTDEVICE
WHERE PAYDEVDATE='27/12/2012') AND (SELECT MIN(ORDERNO)
FROM PAYMENTDEVICE
WHERE PAYDEVDATE='27/12/2012')+10    AND C.CARDID=P.CHARGEDCARDID;

我们orderNo不是每天都重置,而是不断增加,所以我们找到了orderNo当天的最小值并将这个值加 10,以在这些订单号之间找到当天吃的前 10 个人。

那么从这个查询返回什么:

  CARDID
  1005
  1000
  1002
  1003
  1009
  2000
  2001
  1007
  2002
  1004
  1006

其中一些cardId(以1开头)是studentCardId,其中一些(以2开头)是PersonnelCardId。那么我怎样才能匹配和写出相应的名字呢?

4

2 回答 2

1
SELECT *
FROM Personel p INNER JOIN Student s 
ON p.PersonnelCardId = s.StudentCardId 
INNER JOIN ReturnedQuery rq
ON rq.CardId = p.PersonnelCardId

updated:

    SELECT p.PersonnelName, rq.CardId
    FROM Personel p INNER JOIN ReturnedQuery rq
    ON rq.CardId = p.PersonnelCardId 

    UNION

    SELECT s.StudentName, rq.Cardid
    FROM Student s INNER JOIN ReturnedQuery rq 
    ON s.StudentCardId = rq.Cardid 
于 2012-12-27T19:48:33.690 回答
1

您的原始查询实际上非常脆弱。我会像这样重写它(并添加所需的连接):

WITH First_Daily_Purchase as (SELECT chargedCardId,
                                     MIN(payDevTime) as payDevTime, 
                                     MIN(orderNo) as orderNo
                              FROM PaymentDevice
                              WHERE payDevDate >= 
                                              TO_DATE('2012-12-27', 'YYYY-MM-DD')
                                    AND payDevDate <
                                              TO_DATE('2012-12-28', 'YYYY-MM-DD')
                              GROUP BY chargedCardId),
     First_10_Daily_Purchasers as (SELECT chargedCardId
                                   FROM (SELECT chargedCardId, 
                                                RANK() OVER(ORDER BY payDevTime,
                                                                 orderNo) as rank
                                         FROM First_Daily_Purchase) a
                                   WHERE a.rank < 11)
SELECT a.chargedCardId, b.personnelName, b.personnelSurname
FROM First_10_Daily_Purchasers a
JOIN Personnel b
ON b.personnelCardId = a.chargedCardId
UNION ALL
SELECT a.chargedCardId, b.studentName, b.studentSurname
FROM First_10_Daily_Purchasers a
JOIN Student b
ON b.studentCardId = a.chargedCardId

(有一个有效的 SQL Fiddle - 通常防弹这花了我一段时间。)
这应该让你获得前 10 个购买的人(而不是前11 个购买,这是你实际得到的)。这当然假设它payDevTime实际上是以可排序的格式存储的(如果不是你有比这个查询不能正常工作更大的问题)。

也就是说,您的架构设计有许多令人不安的事情。

于 2012-12-27T21:42:43.370 回答