Php 获取订单号,其中订单号有两个时间x值

Php 获取订单号,其中订单号有两个时间x值,php,mysql,sql,Php,Mysql,Sql,我在我的网站上使用的是旧MySql版本和php版本4的Oscommeerce 我的表格结构: | orders_status_history_id | orders_id | orders_status_id | | 1 | 22 | 3 | | 2 | 23 | 2 | | 3

我在我的网站上使用的是旧MySql版本和php版本4的Oscommeerce

我的表格结构:

| orders_status_history_id |  orders_id |  orders_status_id |
|            1             |      22    |         3         |
|            2             |      23    |         2         |
|            3             |      24    |         5         |
|            4             |      75    |         5         |
|            5             |      25    |         5         |
|            6             |      30    |         1         |
|            7             |      26    |         2         |
|            8             |      75    |         54        |
|            10            |      22    |         5         |
|            15            |      48    |         6         |
|            16            |      52    |         8         |
|            17            |      32    |         9         |
|            18            |      75    |         54        |
|            19            |      24    |         5         |
|            22            |      20    |         8         |
|            23            |      75    |         54        |
|            ..            |      ..    |         ..        |
|            ..            |      ..    |         ..        |
我的表总共有200多万行

订单\状态\历史\ id列是自动递增的

所以我的问题是:

如果订单状态id有两个时间id,如何获取订单id

例如,枫叶: 如果orders\u id 75具有orders\u status\u id=54,则应向我显示其orders\u id 75,如果没有,则跳过它

因此,我希望输出如下:

|     orders_status_id     |  orders_id | 
|            54            |      75    | 
我有以下几种方法:

$sql = "SELECT 
            orders_status_history_id, orders_id, orders_status_id 
        FROM 
            orders_status_history 
        Where 
            (orders_status_history_id > ".$dal." AND orders_status_history_id < ".$al.") 
        AND 
            orders_id IN(
                    Select Distinct
                        orders_id
                    From 
                        orders_status_history 
                    Where  
                        (orders_status_history_id > ".$dal." AND orders_status_history_id < ".$al.")
                    HAVING
                        count(orders_status_id=54)>2
                )";
不起作用

SELECT 
    orders_status_history_id, orders_id, orders_status_id 
    FROM 
        orders_status_history 
    WHERE 
        (orders_status_history_id > 0 AND orders_status_history_id < 500)
    AND
        orders_id IN (
            Select 
                orders_id 
            FROM 
                orders_status_history 
            WHERE
                (orders_status_history_id > 0 AND orders_status_history_id < 1000)
            AND  
                count(orders_status_id=54) >2
        )
我找不到获取所需结果的查询

我到底做错了什么

提前感谢。

我将使用group by并拥有:

如果确实需要完整的详细信息,请使用join或in:

试试这个:

SELECT orders_status_history_id, orders_id, orders_status_id
FROM orders_status_history
WHERE (orders_status_history_id > 0 AND orders_status_history_id < 500) and orders_id IN (
    SELECT orders_id
    FROM orders_status_history
    GROUP BY orders_id
    HAVING COUNT(orders_status_history_id) > 1
)
像这样

SELECT orders_id, orders_status_id
FROM test
AS r
WHERE (
    SELECT count(*) FROM test
    WHERE orders_id=r.orders_id AND orders_status_id=r.orders_status_id
)>=2
GROUP BY orders_status_id

希望能奏效。

分组人在哪里?@gordonLinoff它给了我一个错误:查询执行被中断我有1900000多行table@jarlh . . . 它现在在那里。
SELECT orders_status_history_id, orders_id, orders_status_id
FROM orders_status_history
WHERE (orders_status_history_id > 0 AND orders_status_history_id < 500) and orders_id IN (
    SELECT orders_id
    FROM orders_status_history
    GROUP BY orders_id
    HAVING COUNT(orders_status_history_id) > 1
)
SELECT orders_id, orders_status_id
FROM test
AS r
WHERE (
    SELECT count(*) FROM test
    WHERE orders_id=r.orders_id AND orders_status_id=r.orders_status_id
)>=2
GROUP BY orders_status_id