gpt4 book ai didi

mysql - 选择行直到列值不相同

转载 作者:行者123 更新时间:2023-11-29 10:55:37 24 4
gpt4 key购买 nike

SELECT product.productID
, product.Name
, product.date
, product.status
FROM product
INNER JOIN shelf ON product.sheldID=shelf.shelfID
WHERE product.weekID = $ID
AND product.date < '$day'
OR (product.date = '$day' AND shelf.expire <= '$time' )
ORDER BY concat(product.date,shelf.expire)

我试图在特定值处停止 SQL 语句,例如不好。

我尝试过使用 max-date,但发现它很难,因为我在查询中添加时间戳。 (组合日期/时间)

此示例表显示应返回 3 个结果,如果状态“bad”是第一个结果,则不应返回任何结果。 (它们按日期和时间排序)。

ProductID   Date     status
1 2017-03-27 Good
2 2017-03-27 Good
3 2017-03-26 Good
4 2017-03-25 Bad
5 2017-03-25 Good

我想我可能已经修复了它,我将其添加到我的 while 循环中。

查询使用日期和时间按现在到过去的顺序给出结果,这个 while 循环检查该行的列是否等于“坏”(如果它做了某些事情)(可能可以使用数组来填充用数据来解决)。如果不是,则循环已损坏。

我知道这看起来并不理想,但它确实有效,哈哈

   while ($row = mysqli_fetch_assoc($result)) {
if ($row['status'] == "bad") {
$counter += 1;
}
else{
break;}

最佳答案

我将仅用您的输出提供答案,就好像它只是一张表一样。它将为您提供如何解决问题的主要想法。

基本上,我创建了一个名为 ord 的列,它将用作 row_number (据我所知,MySql 还不支持它)。然后我得到了不良状态的最小 ord 值,然后我从 ord 小于该值的数据中获取了所有内容。

select y.* 
from (select ProductID, dt, status, @rw:=@rw+1 ord
from product, (select @rw:=0) a
order by dt desc) y
where y.ord < (select min(ord) ord
from (select ProductID, status, @rin:=@rin+1 ord
from product, (select @rin:=0) a
order by dt desc) x
where status = 'Bad');

结果将是:

ProductID        dt     status  ord
-------------------------------------
1 2017-03-27 Good 1
2 2017-03-27 Good 2
3 2017-03-26 Good 3

还使用 Bad 状态为第一个结果的用例进行了测试,不会返回任何结果。

在这里查看它的工作情况:http://sqlfiddle.com/#!9/28dda/1

关于mysql - 选择行直到列值不相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43082029/

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