我有一个mysql表。
name | total
chris | 5
jan | 3
bob | 2
eric | 4chris和jan是用下面的代码选择的
$query = " select * from table where name = '$given_name' ";
// &given_name = result from another query我想将查询结果存储到一个变量中。
while($row=mysql_fetch_assoc($query)){
$result = $row['name'];
} // i want to store both chris and jan to $result然后,我将使用查询的结果进行另一个查询。我想选择其余的名称。而不是第一个查询中的那些。我不希望在此查询中选择克里斯和简,因为它存储在$result中
select * from table where name != $result ;但是有一个名字只存储在$result中。我希望它们都存储在$result中。
发布于 2018-08-10 10:14:15
您可以使用FIND_IN_SET查看以前是否获取过这些名称。首先,您需要让$result成为一个包含所有名称的数组:
$result = array();
while ($row=mysql_fetch_assoc($query)) {
$result[] = $row['name'];
}然后,您可以编写查询以排除$result中的名称
$sql = "SELECT * FROM table WHERE NOT FIND_IN_SET(name, '" . implode(',', $result) . "')";发布于 2018-08-10 10:19:34
您可以使用如下数据库查询来排除chris和jan
select * from table where name NOT IN( $result );发布于 2018-08-10 10:58:04
假设您不知道结果集的名称,您可以简单地(a)从结果集中选择前两个名称,(b)将它们连接在一个字符串中,最后(c)使用"NOT IN“作为查询参数。
$numPicks = 2; // decide how many names you want in the list
// OR if you want to exclude ALL names found in the first query
$num_rows = mysql_num_rows($query);
$nameList = ''; // start with an empty string
for($i=0; $i<$numPicks; $i++) { // then use $num_rows here instead of numPicks
$nameList .= $row['name'].',';
}
$nameList = rtrim($nameList,','); // remove trailing comma from the string
$sql = "select * from table where name NOT IN ($nameList)";https://stackoverflow.com/questions/51777889
复制相似问题