mysql id数组,MySQL,MySQL:从ID不位于数组中的表中选择*

so I currently have a database table where I am trying to select all of the records EXCEPT for those that are included in an array that I have made. As some background context:

The structure of the database table in question is:

server_status:

id int(11)

server_id int(11)

time_checked datetime

status char(1)

My PHP script to get the data into the hash looks like this:

$sql2 = "SELECT server_id, time_checked,id from server_status where time_checked

$result2=$conn->query($sql2);

while($row2 = $result2->fetch_assoc()){

$server_id = $row2['server_id'];

$id = $row2['id'];

$dt = $row2['time_checked'];

$year = substr($dt,0,4);

$month = substr($dt,5,2);

$day = substr($dt,8,2);

$day = "$year-$month-$day";

$all[$server_id][$day] = $id; // ARRAY

}

So what I'm trying to do is create a MySQL query that reads in the ids ($id) from the array, and selects * APART from that. From looking it up, it seems like I will have to use a 'where not' clause, but I do not know how to reference the hash in this.

Further clarification: I now have an array which extracts data which looks like this:

1{

2016-05-05 : 252

2016-05-10 : 406

2016-04-27 : 141

2016-05-04 : 164

2016-05-09 : 263

2016-05-03 : 153

2016-04-26 : 131

2016-04-14 : 1

2016-04-18 : 31

2016-04-21 : 111

2016-04-20 : 61

2016-04-19 : 51

2016-04-15 : 21

2016-04-25 : 121

}

2{

2016-05-10 : 452

2016-05-05 : 198

2016-05-09 : 264

2016-05-04 : 165

2016-04-26 : 132

2016-04-27 : 143

2016-04-25 : 122

2016-04-21 : 112

2016-05-03 : 154

}

I want to take the IDs from this array (e.g. 154) and select everything in the table which doesn't have any of the above IDs. I hope this helps to clarify?!

Any help will be greatly appreciated!

解决方案

SOLVE:

$sql2 = "SELECT server_id, time_checked,id from server_status where time_checked

$result2=$conn->query($sql2);

while($row2 = $result2->fetch_assoc()){

while($row2 = $result2->fetch_assoc()){

$server_id = $row2['server_id'];

$id = $row2['id'];

$dt = date('Y-m-d', strtotime($row2['time_checked']));

$all[$server_id][$dt] = $id; // ARRAY

}

}

$stack = array();

$keys = array_keys($all);

for($i = 0; $i < count($all); $i++) {

foreach($all[$keys[$i]] as $key => $value) {

array_push($stack, $value);

}

}

$ids = join(',',$stack);

$sql = "SELECT * FROM server_status WHERE time_checked

$result=$conn->query($sql);

echo "Server status data has been deleted.
";

Created another array from the multi-dimensional array to store the ids only and use NOT IN like John Green suggested.

Thanks!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值