MySQL查询冻结

时间:2016-07-22 11:01:14

标签: mysql sql

我有以下查询

    SELECT DISTINCT
    Products.Product_name
    FROM Order_details
    INNER JOIN Products
    ON Products.Product_codenumber = Order_details.Product_codenumber
    INNER JOIN Orders
    ON Order_details.Order_ID = Orders.Order_ID
    INNER JOIN Customers
    ON Orders.Customer_ID = Customers.Customer_ID
    INNER JOIN Discount_cards
    ON Discount_cards.Phone_number = Customers.Login
    WHERE Orders.Status_ID = '5'
    AND (DATE(Orders.Time) BETWEEN '$start_date' AND '$end_date')

旨在向我提供折扣卡用户从订单信息订购的范围或产品。当我第一次写它时,它似乎工作正常,但最近我被告知它冻结了,我不知道为什么。我试着用它玩一点,似乎当我添加日期范围,或者甚至只是单个日期搜索条件时,它会冻结(可以显示'正在加载'永远存在于PHPMyAdmin中);除此之外,执行需要大约1秒钟。

registry validation

1 个答案:

答案 0 :(得分:0)

好吧,显然抛弃了Discount_cards的内连接并用WHERE IN替换它修复了这个问题:

    SELECT DISTINCT
    Products.Product_name
    FROM Order_details
    INNER JOIN Products
    ON Products.Product_codenumber = Order_details.Product_codenumber
    INNER JOIN Orders
    ON Order_details.Order_ID = Orders.Order_ID
    INNER JOIN Customers
    ON Orders.Customer_ID = Customers.Customer_ID
    WHERE 
    Customers.Login IN (
        SELECT
        Phone_number
        FROM Discount_cards
        )
    AND Orders.Status_ID = '5'
    AND (DATE(Orders.Time) BETWEEN '$start_date' AND '$end_date')

我想知道为什么会发生这种冻结,我将不胜感激。