Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
create table first_table ( username varchar(64) primary key, first_que int, last_que int ); insert into first_table values ('usera', 101, 200), ('userb', 201, 300), ('userc', 301, 400); create table second_table ( username varchar(64) references first_table(username), submitted_question_number int, `timestamp` varchar(10) ); insert into second_table values ('usera', 103, '13-06-2022'), ('usera', 102, '11-06-2022'), ('userb', 201, '12-06-2022'), ('userc', 101, '09-06-2022');

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php $query = 'SELECT first_table.username, (last_que-first_que) total_questions_allotted, (last_que-first_que-answers) pending_questions FROM first_table JOIN ( SELECT username, COUNT(*) answers FROM second_table GROUP BY username ) second_table_groupped USING(username)'; // Run query using mysqli $result = $mysqli->query($query); $data = $result->fetch_all(MYSQLI_ASSOC); print_r($data);
Copy Clear