简体   繁体   English

SQL查询匹配两个字段

[英]SQL Query matching two fields

I currently have a table called cc_site , in this table I store the results of pings from different ip addresses. 我目前有一个名为cc_site的表,该表中存储了来自不同IP地址的ping结果。

The table has four fields: 该表具有四个字段:

  • auto increment ID field 自动递增ID字段
  • date (which includes time) 日期(包括时间)
  • IP address IP地址
  • status (used to determine if the ping was successful or not). 状态(用于确定ping是否成功)。

I'm trying to create a query which will give me the latest result based off the date field for an individual ip address. 我正在尝试创建一个查询,该查询将根据单个IP地址的日期字段为我提供最新结果。 The code I'm currently using is below but it doesn't work unless the IP I'm using in the query is the latest entry in the table. 我当前使用的代码在下面,但除非查询中使用的IP是表中的最新条目,否则它将无法正常工作。

$query = "SELECT *
          FROM cc_site
          WHERE ip='$host'
          AND Date IN (
              SELECT max(date)
              FROM cc_site
          )";

I knew the query is incorrect however I have not been able to find code that would perform the query I need. 我知道查询不正确,但是我无法找到执行所需查询的代码。

Any help would be great. 任何帮助都会很棒。

No need of that sub-query. 无需该子查询。 Use can use ORDER and LIMIT instead - 使用可以改为使用ORDERLIMIT

SELECT * FROM cc_site WHERE ip='$host' ORDER BY `date` DESC LIMIT 1

This will sort the records in descending order according to date and return the record with highest record. 这将根据日期以降序对记录进行排序,并返回记录最高的记录。

select * from cc_site 
where ip='$host' and date in(select max(date) from cc_site where ip='$host');

I think this might give you the answer 我认为这可能会给您答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM