对左连接查询进行排序

时间:2012-08-08 00:22:52

标签: mysql join

我有3个表:domainsstatusdomain_status

mysql> desc domains;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| domain_id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| domain_url    | varchar(255) | YES  |     | NULL    |                |
| domain_active | int(1)       | YES  |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+

mysql> desc status;
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| status_id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| response_time | varchar(11) | YES  |     | NULL    |                |
| time_pinged   | int(11)     | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+

mysql> desc domain_status;
+------------------+---------+------+-----+---------+----------------+
| Field            | Type    | Null | Key | Default | Extra          |
+------------------+---------+------+-----+---------+----------------+
| domain_status_id | int(11) | NO   | PRI | NULL    | auto_increment |
| domain_id        | int(11) | YES  |     | NULL    |                |
| status_id        | int(11) | YES  |     | NULL    |                |
+------------------+---------+------+-----+---------+----------------+

数据

mysql> SELECT * FROM domains;
    +-----------+----------------+---------------+
    | domain_id | domain_url     | domain_active |
    +-----------+----------------+---------------+
    |         1 | yahoo.com      |             1 |
    |         2 | google.com     |             1 |
    +-----------+----------------+---------------+
    2 rows in set

    mysql> SELECT * FROM status;
    +-----------+---------------+-------------+
    | status_id | response_time | time_pinged |
    +-----------+---------------+-------------+
    |         1 | 143           |  1344373818 |
    |         2 | 0             |  1344373945 |
    |         3 | 40            |  1344374045 |
    |         4 | 504           |  1344375047 |
    +-----------+---------------+-------------+
    4 rows in set

    mysql> SELECT * FROM domain_status;
    +------------------+-----------+-----------+
    | domain_status_id | domain_id | status_id |
    +------------------+-----------+-----------+
    |                1 |         1 |         1 |
    |                2 |         1 |         2 |
    |                3 |         2 |         3 |
    |                4 |         2 |         4 |
    +------------------+-----------+-----------+

我正在尝试列出域并在time_pinged表中显示最高status行的状态记录。我尝试使用group by的MAX函数,但只有在没有其他列数据显示时才有效,这不是这里的情况。

现在查询显示4行,我现在已经尝试了几个小时,让它显示2行,同时显示最高time_pinged

查询:

SELECT
    domains.domain_id,
    domain_url,
    response_time,
    time_pinged,
    domain_active
FROM
    (domains)
LEFT JOIN domain_status ON domain_status.domain_id = domains.domain_id
LEFT JOIN status ON status.status_id = domain_status.status_id
WHERE
    (
        domains.domain_id LIKE '%%'
        OR domain_url LIKE '%%'
        OR response_time LIKE '%%'
        OR time_pinged LIKE '%%'
        OR domain_active LIKE '%%'
    )
ORDER BY
    domain_id ASC
LIMIT 25

查询结果:

+-----------+----------------+---------------+-------------+---------------+
| domain_id | domain_url     | response_time | time_pinged | domain_active |
+-----------+----------------+---------------+-------------+---------------+
|         1 | dhhcontrol.com | 143           |  1344373818 |             1 |
|         1 | dhhcontrol.com | 0             |  1344373945 |             1 |
|         2 | google.com     | 504           |  1344375047 |             1 |
|         2 | google.com     | 40            |  1344374045 |             1 |
+-----------+----------------+---------------+-------------+---------------+
4 rows in set

对不起,如果它有点长的啰嗦,我已尝试在连接中选择等等。我无法弄明白。谢谢!

1 个答案:

答案 0 :(得分:3)

我认为您需要额外的subquery才能获得最大值,然后将它们与您的表格连接起来。试试这个:(INNER JOIN将执行此操作,因为只会检索具有最高ping的所有域

SELECT  y.domain_id, 
        y.domain_url,
        w.response_time,
        w.time_pinged,
        y.domain_active
FROM    domains y INNER JOIN
            (
                SELECT  a.domain_id, MAX(c.time_pinged) MaxPing
                FROM    domains a 
                            INNER JOIN domain_status b
                                on a.domain_id = b.domain_id
                            INNER JOIN `status` c
                                on b.status_id = c.status_ID
                GROUP BY a.domain_id
            ) z ON  y.domain_id = z.domain_id
            INNER JOIN domain_status x
                ON  y.domain_id = x.domain_id
            INNER JOIN `status` w
                ON  x.status_id = w.status_ID AND
                    z.MaxPing = w.time_pinged
-- WHERE ... your condition
ORDER BY domain_id ASC
LIMIT 25