简体   繁体   English

MySQL查询在一个查询中检查多个记录

[英]MySQL Query Checking Multiple Records In One Query

I have the following data in php: 我在php中有以下数据:

ID & Timestamps for each blog 每个博客的ID & Timestamps

Then I have a table which contains: 然后我有一个表,其中包含:

| id | blog | timestamp |

I need to find out which of my php timestamp data does not equal the timestamp in the mysql database. 我需要找出哪个php时间戳数据等于mysql数据库中的timestamp

Sure I could run a hundred queries for each and every timestamp I have in php to look if it is changed, but that seems inefficient. 当然,对于php中的每个时间戳,我都可以运行一百个查询,以查看是否已更改,但这似乎效率很低。

Is there any way to put a single query in Mysql to get the data? 有什么方法可以在Mysql中放置一个查询以获取数据?

To clarify, my php ID & timestamps are in an array like 澄清一下,我的php ID & timestamps在一个数组中,例如

Array ( [3] => Array ( [timestamp] => 1389414084 ) ) 

I need to check in mysql if the record with ID=3 has the timestamp of 1389414084. 我需要检查mysql,如果ID = 3的记录的时间戳为1389414084。

Problem is, with a lot of data to check this would end up being a huge number of queries. 问题是,要检查大量数据,最终将导致大量查询。 And I only need the ones that do not match. 我只需要不匹配的那些。

I have no idea how to go about this so any help would be very appreciated. 我不知道该如何处理,因此我们将不胜感激。

To clarify - the first data is json. 为了澄清-第一个数据是json。 That's how it has to be. 那就是必须的。 I decode it and then have to match it up to the blogs timestamp. 我将其解码,然后必须将其与博客时间戳匹配。

What I would suggest is make hash of ID, Blog & Timestamp and store it in the table itself. 我的建议是对ID,Blog和Timestamp进行哈希处理并将其存储在表本身中。 And instead of checking id & timestamp, you may check for the hash match (select * from my BlogTimeStamp where hash NOT IN ('hash1', 'hash2', ....). 而不是检查ID和时间戳,您可以检查哈希值是否匹配(从我的BlogTimeStamp中选择*,其中哈希值不输入('hash1','hash2'...)。

I don't know your exact requirement. 我不知道您的确切要求。 maybe there would be a better solution altogether 也许会有更好的解决方案

SELECT * 
FROM myBlogTable
WHERE timestamp NOT IN (1389414084);

If you use a select statement to get the 1389414084 then insert that in the parentheses instead of hard coding in the timestamp so that the query is dynamic if you ever need it to be. 如果使用select语句获取1389414084则将其插入括号中,而不是在时间戳中进行硬编码,以便在需要时动态查询。

Here's what I did in the end. 这就是我最后所做的。 I just broke down the query and constructed it so that I had everything in one query. 我只是分解查询并构造它,以便将所有内容都放在一个查询中。

SELECT * FROM title WHERE id IN((SELECT id FROM title WHERE id = '75' AND date ='1392501995'),(SELECT id FROM title WHERE id = '74' AND date ='1392401481')) 

So you just create it in php. 因此,您只需在php中创建它。

$i = count($fav);
$sql = 'SELECT * FROM title WHERE id IN(';
    foreach($fav as $key => $val){
        $sql .= "(SELECT id FROM title WHERE id = '".$key."' AND date ='".$val["date"]."')"; 
        if($i > 1) {
            $sql .= ",";
            }
        $i = $i-1;
    }
    $sql .= ")";

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

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