我有一个包含两列的sql数据库 1. id和2. parent_id
我想选择parent_id = 1的所有id [让结果保存在X数组中]然后, 选择X []中的parent_id所有的id [让结果保存在Y数组]然后...最后做7个等级我想要对所有级别求和并以变量名“number”得到该总和
我尝试了什么:worker_processes auto;
worker_rlimit_nofile 100000;
events {
worker_connections 5000;
multi_accept on;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10;
keepalive_requests 1024;
client_body_timeout 12;
client_header_timeout 12;
send_timeout 10;
proxy_cache_path /tmp/mycache keys_zone=mycache:10m use_temp_path=off;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name localhost;
access_log off;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
open_file_cache max=10000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors on;
client_body_buffer_size 16K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 2 1k;
location / {
proxy_cache mycache;
proxy_max_temp_file_size 1924m;
slice 100m;
proxy_cache_key $host$uri$slice_range;
proxy_set_header Range $slice_range;
proxy_http_version 1.1;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_cache_valid any 1d;
limit_conn addr 5;
proxy_pass http://domain2.com/;
secure_link $arg_md5,$arg_expires;
secure_link_md5 "secret$secure_link_expires$uri";
if ($secure_link = "") { return 403; }
if ($secure_link = "0") { return 410; }
}
}
}
然后再次
$sql = "SELECT count(id) as leadersearch FROM `$database`.`$mem` where parent_id = ".$parent[id];
$result = mysqli_query($con, $sql);
$lv1 = mysqli_fetch_array($result);
依此类推....它可能会给我所需的结果,但代码太多...... 我希望通过使用循环或其他东西来缩短代码。
答案 0 :(得分:0)
您的第一个查询获取计数(id),然后第二个查询使用它来获取结果。你的要求似乎有许多缺陷。
但是,对于您的问题,您可以使用如下的
子查询$sql = "SELECT count(id) as leadersearch
FROM `$database`.`$mem`
WHERE parent_id in (
SELECT id FROM `$database`.`$mem` where parent_id = ".$parent['id']."
)";
$result = mysqli_query($con, $sql);
$lv2 = mysqli_fetch_array($result);
如果您有多个级别嵌套并且想要搜索这样的记录,那么您可以考虑函数:
function getLeader($parent_id){
$results = [];
$sql = "SELECT id as leadersearch FROM `$database`.`$mem` where parent_id in (".$parent_id.")";
$result = mysqli_query($con, $sql);
foreach($row = mysqli_fetch_array($result)){
$results[] = $row['id'];
}
return $results;
}
$leaders = [];
$ids = getLeader($parent['id']);
foreach($ids as $id){
$leaders[$id] = getLeader($id);
}