gpt4 book ai didi

MySQL 查询卡住

转载 作者:行者123 更新时间:2023-11-29 20:43:06 25 4
gpt4 key购买 nike

我有一个以下查询

    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 秒。

Here's what EXLAIN gives me.

最佳答案

好吧,显然丢弃 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')

我想知道为什么会发生卡住,我们将不胜感激。

关于MySQL 查询卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38524803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com