简体   繁体   English

检查mysql查询结果以运行第二个查询,该查询在第一个查询结果中不可用

[英]Check mysql query result to run second query which is not available in first query result

I have this code: 我有以下代码:

$sql = "SELECT ns, id FROM `dns` WHERE `ns` = 1 LIMIT 0, 30 ";       
$result = mysql_query($sql);

// After this I need to run second query which is not available in first query. Help me with this please.
$sql2 = "SELECT ns, id FROM `dns` WHERE `ns` = 1 AND `id` = not available in first query, how to do that? LIMIT 0, 30 ";         
$result = mysql_query($sql);

// I can then echo my first query result here 
// I can then echo my second query result here 

What I need to do is to check the first result to run the second query. 我需要做的是检查第一个结果以运行第二个查询。 I set the limit on purpose. 我故意设置了限制。 For example if the id from 1 to 30 is available in first query. 例如,如果第一个查询中的id为1到30。 The second query will query id for example starting from 31 to 60. 第二个查询将查询id ,例如从31到60。

I hope you can understand what I'm trying to say. 希望您能理解我要说的话。 Thanks in advance. 提前致谢。

UPDATE : I need to make dig lookup with the query result. 更新 :我需要使用查询结果进行挖掘查找。 The query result is nameserver. 查询结果是名称服务器。 Because dig response speed is random so I was thinking I want to split for example: 30 result from 90 nameserver and run 3 simultaneous dig lookup. 由于挖掘响应速度是随机的,因此我想例如进行拆分:从90个域名服务器中提取30个结果,并同时运行3个挖掘查找。 So thats why the second query (nameserver) result should not available in the first query result and the third query result should not available in the second query result. 因此,这就是为什么第二个查询(名称服务器)结果在第一个查询结果中不可用而第三个查询结果在第二个查询结果中不可用的原因。 I need to use all 3 result for later use. 我需要使用所有3个结果供以后使用。 I realy appreciate your response. 非常感谢您的回复。 Thank you. 谢谢。

You can just change the LIMIT criteria. 您可以更改LIMIT条件。 There's an "OFFSET" option (which you've specified as 0 in your first query). 有一个“偏移”选项(您在第一个查询中将其指定为0)。 Using your examples the following would suffice. 使用您的示例,以下内容就足够了。

$sql = "SELECT ns, id FROM `dns` WHERE `ns` = 1 LIMIT 0, 30 ";       
$result = mysql_query($sql);

// After this I need to run second query which is not available in first query. Help me with this please.
$sql2 = "SELECT ns, id FROM `dns` WHERE `ns` = 1 LIMIT 30, 30 ";         
$result = mysql_query($sql);

The second query will start at offset by 30 and return 30 more rows, so in this case rows 31-60 第二个查询将从偏移量开始30,然后再返回30行,因此在本例中为第31-60行

edit Based on your updated question, a more sensible approach would be to simply do 您更新基于问题的修改 ,更明智的方法是简单地做

$sql = "SELECT ns, id FROM `dns` WHERE `ns` = 1 LIMIT 0, 90 ";       
$result = mysql_query($sql);

And then have PHP split up the rows into requests to separate servers as required thus ensuring there are no duplicates and reducing back and forth between PHP and MySQL. 然后让PHP根据需要将行拆分为多个请求,以分离服务器,从而确保没有重复,并减少了PHP和MySQL之间的来回往返。

If you just need to grab the next 30 rows, why not just: 如果您只需要获取接下来的30行,为什么不这样做:

LIMIT 30, 30

On the second query? 关于第二个查询?

See http://dev.mysql.com/doc/refman/5.1/en/select.html#id838627 regarding LIMIT : 有关LIMIT请参见http://dev.mysql.com/doc/refman/5.1/en/select.html#id838627

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. 有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。

Note that this can also be written as LIMIT 30 OFFSET 30 if you want to be more clear. 请注意,如果您想更清楚一点,也可以将其写为LIMIT 30 OFFSET 30 So: 所以:

$sql = "SELECT ns, id FROM `dns` WHERE `ns` = 1 LIMIT 30 OFFSET 30";
$result = mysql_query($sql);

If your ids/queries were just examples, and what you really need is a query that selects from a set that excludes the result set from a different query, then you can use a sub-query like this: 如果您的ID /查询仅是示例,而您真正需要的是一个从集合中选择的查询,该查询从另一个查询中排除了结果集,那么您可以使用这样的子查询:

SELECT ns, id
FROM `dns`
WHERE `ns` = 1
AND `id` NOT IN (
    SELECT `id`
    FROM `dns`
    WHERE `ns` = 1
    LIMIT 0, 30
)
LIMIT 0, 30

First the inner sub-query will run and return a list of ids. 首先,内部子查询将运行并返回ID列表。 That set of ids will be explicitly excluded from the outer query because we specify NOT IN (...) . 由于我们指定了NOT IN (...)因此将从外部查询中明确排除该组ID。

Use NOT IN with a subquery, like so: NOT IN与子查询一起使用,如下所示:

SELECT ns, id FROM `dns` WHERE `ns` = 1 AND `id` NOT IN 
(SELECT ns, id FROM `dns` WHERE `ns` = 1 LIMIT 0, 30) 
LIMIT 0, 30

So what's your question? 那你有什么问题

And why don't you just do a subselect or join? 而且为什么不只是进行子选择或加入?

Why not just do the first query without a limit and then extract the first 30 results with a for loop or something. 为什么不做没有限制的第一个查询,然后使用for循环之类的东西提取前30个结果呢?

Saves doing two queries. 节省做两个查询。

So 所以

$query = SELECT ns, id FROM dns WHERE ns = 1
$result = mysql_query($query);

for ($i=0;$i<30;$i++) {
//Do Stuff with mysql_fetch_row...
}

for ($i=0;$i<30;$i++) {
//Do more stuff
}

Unless there is a reason you can't get all the results at once? 除非有原因,否则您无法一次获得所有结果?

EDIT: Althought clearly you could put LIMIT 60 in the query if you definitely only wanted 60 results. 编辑:虽然很明显,如果您绝对只想要60个结果,则可以在查询中输入LIMIT 60

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

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