我有 3 张桌子:domains
, status
, domain_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
抱歉,如果它有点啰嗦,我已经尝试在连接等中选择。我无法弄清楚。谢谢!