PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table rsbl_orders_list (id int primary key, det_id int, order_status varchar ); insert into rsbl_orders_list values (1148526,12052517,'NW'); insert into rsbl_orders_list values (1148594,12052517,'WP'); insert into rsbl_orders_list values (1148397,12053064,'NW'); insert into rsbl_orders_list values (1148741,12053064,'WP'); insert into rsbl_orders_list values (1148462,12054629,'WP'); insert into rsbl_orders_list values (1148669,12054629,'NW'); insert into rsbl_orders_list values (1148519,12055541,'WP'); insert into rsbl_orders_list values (1148575,12055541,'NW'); insert into rsbl_orders_list values (1148391,12055737,'CP'); insert into rsbl_orders_list values (1148823,12055737,'NW'); insert into rsbl_orders_list values (1148548,12069635,'WP'); insert into rsbl_orders_list values (1148603,12069635,'NW'); insert into rsbl_orders_list values (1148447,12245289,'WP'); insert into rsbl_orders_list values (1148650,12245289,'NW'); insert into rsbl_orders_list values (1148497,12245648,'NW'); insert into rsbl_orders_list values (1148702,12245648,'WP'); insert into rsbl_orders_list values (1148392,12247299,'WP'); insert into rsbl_orders_list values (1148743,12247299,'NW'); insert into rsbl_orders_list values (1148389,12248107,'WP'); insert into rsbl_orders_list values (1148755,12248107,'NW'); insert into rsbl_orders_list values (1148486,12248267,'NW'); insert into rsbl_orders_list values (1148621,12248267,'CP'); insert into rsbl_orders_list values (3528104,27819164,'CP'); insert into rsbl_orders_list values (3528105,27819164,'CP'); select * from rsbl_orders_list order by det_id;
Copy Clear
Copy Format Clear
<?php use Carbon\Carbon; $now = Carbon::now()->format('d/m/Y'); /* printf("Today is %s\nCurrent PHP version: %s \n\n", $now, phpversion()); $query = "SELECT VERSION() as version;"; // get DB version using PDO $stmt = $pdo->prepare($query); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); printf('DB version (PDO): %s ' . PHP_EOL, $row['version']); // Run query using mysqli $result = $mysqli->query($query); $version = $result->fetch_object(); printf('DB version (mysqli): %s ' . PHP_EOL, $version->version); exit;*/ $q="select count(*) as t, det_id from rsbl_orders_list group by det_id having count(*) >1 order by det_id;"; $stmt = $pdo->prepare($q); $stmt->execute(); $rows = query_array($stmt, $q); $to_delete=array(); foreach($rows as $r){ $q2="select * from rsbl_orders_list where det_id=".$r["det_id"]; $stmt = $pdo->prepare($q2); $stmt->execute(); $rows2 = query_array($stmt, $q2); $id_ok= (!is_null(array_column($rows2, 'id', 'order_status')['CP']) ? array_column($rows2, 'id', 'order_status')['CP'] : ( !is_null(array_column($rows2, 'id', 'order_status')['WZ']) ? array_column($rows2, 'id', 'order_status')['WZ'] : ( !is_null(array_column($rows2, 'id', 'order_status')['EC']) ? array_column($rows2, 'id', 'order_status')['EC'] : ( !is_null(array_column($rows2, 'id', 'order_status')['WP']) ? array_column($rows2, 'id', 'order_status')['WP'] : ( !is_null(array_column($rows2, 'id', 'order_status')['NW']) ? array_column($rows2, 'id', 'order_status')['NW'] : "ERRORE") ) ) )); //echo $r["det_id"]." ".$result."\n"; if($id_ok!="ERRORE"){ foreach($rows2 as $r2) if($r2["id"]!=$id_ok) $to_delete[]=$r2["id"]; } else echo $r["det_id"]." ERRORE\n"; } echo (json_encode($to_delete)); function query_array($db,$q){ $res = $db->fetchAll(PDO::FETCH_ASSOC); //var_dump($res); if (!$res){ echo "Errore in query: $q \n"; var_dump($db->errorInfo()); exit(1); } $lista=array(); foreach ($res as $row){ $lista[]=$row; } return $lista; }
Show:  
Copy Clear