SQL根据第二个表在一个表中查找结果

时间:2019-05-14 21:46:54

标签: sql

我有两个表,我需要查找当天已准备好并且INumber中没有句号并且尚未交付所有项目的所有发票。每个发票的Invdetails中可能有几行,我必须找到数量不为零的所有行。

public const float TRANSITION_TIME = 30;
public float transitionTimeElapsed = TRANSITION_TIME;

...

public Color[] backgroundColors;
public int colorIndex;
public int lastColorIndex;

...

public void PrepareBackgroundColors()
{

    backgroundColors = new Color[4];

    backgroundColors[0] = Color.black;
    backgroundColors[1] = new Color(1, 0.85f, 0.62f); //Naranja
    backgroundColors[2] = new Color(0.6f, 0.8f, 1); //Azul
    backgroundColors[3] = new Color(1, 0.72f, 0.29f); //Naranja
    colorIndex = Random.Range(0, 3);

    if (colorIndex == 0)
        lastColorIndex = 3;
    else
        lastColorIndex = colorIndex - 1;

}

...

private void UpdateBackground()
{

    if (transitionTimeElapsed <= Time.deltaTime)
    {
        // start a new transition
        transitionTimeElapsed = TRANSITION_TIME;
        lastColorIndex = colorIndex;
        colorIndex++;

        if (colorIndex > backgroundColors.Length - 1)
        {
            colorIndex = 0;
        }
        Debug.Log(colorIndex + " - " + lastColorIndex);
    }
    else
    {
        // transition in progress
        // calculate interpolated color
        Camera.main.backgroundColor = Color.Lerp(backgroundColors[colorIndex], backgroundColors[lastColorIndex],
            transitionTimeElapsed / TRANSITION_TIME);

        // update the timer
        transitionTimeElapsed -= Time.deltaTime;
    }

}

这是我的SQL,但是需要很长时间,有时会挂起,我不知道是否有更好的方法。

Invoice
Field String: INumber
Field Date: DateReady

InvDetails
Field String: INumber
Field Integer: Quantity

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您还可以在进行联接之前尝试缩小表的范围,因此,您将看到更少的行...

SELECT D.*
FROM (SELECT * FROM Invoice WHERE Dateready = ‘2019-05-14’ AND CHARINDEX(‘.’, INumber) = 0) AS I
LEFT JOIN (SELECT * FROM InvDetails WHERE Quantity > 0) AS D ON I.INumber = D.INumber

答案 1 :(得分:0)

假设此查询是正确的逻辑:

select D.*
from InvDetails D join
     Invoice I 
     on D.INumber = I.INumber
where I.Dateready = '2019-05-14' and
      D.Quantity > 0 and
      I.INumber IS NOT LIKE '%.%';

您希望在以下位置建立索引:

create index idx_invoice_dateready_inumber on (dateread, inumber);

create index idx_invoicedetails_inumber_quantity on (inumber, quantity);

这将大大提高查询的性能。