MySQLQuantity20241125.php


<?php
session_start ();

include ("/home/david/MySQLTEST.php");

$db = new PDO ("mysql: host=localhost", $db_user, $db_password);

$sql = "CREATE DATABASE quantity20241125" . session_id () . "; ";
$db->query ($sql);
try {
    $sql = "USE quantity20241125" . session_id () . "; ";
    $db->query ($sql);

    $sql = "CREATE TABLE a (x INTEGER); CREATE TABLE b (x INTEGER); CREATE TABLE c (x INTEGER); ";
    $db -> query ($sql);

    for ($i = 0;  $i < 24;  $i++) {
        $sql  = "INSERT INTO a (x) VALUES (\"" . rand () %  24 . "\"); ";
        $sql .= "INSERT INTO b (x) VALUES (\"" . rand () %  64 . "\"); ";
        $sql .= "INSERT INTO c (x) VALUES (\"" . rand () % 128 . "\"); ";
        $db -> query ($sql);
    }

    $sql = "SELECT x FROM (
                SELECT x FROM (SELECT x FROM a UNION SELECT x FROM b) x
                    INTERSECT
                SELECT x FROM (SELECT x FROM c) x
        ) x ORDER BY x; ";
    $stmt = $db -> query ($sql);
    while ($row = $stmt -> fetch ())
        echo $row [0] . ", ";
    echo "<br>\n";
    $sql = "SELECT x FROM (
                SELECT x FROM (SELECT x FROM a INTERSECT SELECT x FROM c) x
                    UNION
                SELECT x FROM (SELECT x FROM b INTERSECT SELECT x FROM c) x
        ) x ORDER BY x; ";
    $stmt = $db -> query ($sql);
    while ($row = $stmt -> fetch ())
        echo $row [0] . ", ";
    echo "<br>\n";

    $sql = "SELECT x FROM (
                SELECT x FROM (SELECT x FROM b UNION SELECT x FROM c) x
                    INTERSECT
                SELECT x FROM a
        ) x ORDER BY x; ";
    $stmt = $db -> query ($sql);
    while ($row = $stmt -> fetch ())
        echo $row [0] . ", ";
    echo "<br>\n";

    $sql = "SELECT x FROM (
                SELECT x FROM (SELECT x FROM a INTERSECT SELECT x FROM b) x
                    UNION
                SELECT x FROM (SELECT x FROM a INTERSECT SELECT x FROM c) x
        ) x ORDER BY x; ";
    $stmt = $db -> query ($sql);
    while ($row = $stmt -> fetch ())
        echo $row [0] . ", ";
    echo "<br>\n";

    $sql = "SELECT x FROM (
                SELECT x FROM (SELECT x FROM a UNION SELECT x FROM c) x
                    INTERSECT
                SELECT x FROM b
        ) x ORDER BY x; ";
    $stmt = $db -> query ($sql);
    while ($row = $stmt -> fetch ())
        echo $row [0] . ", ";
    echo "<br>\n";

    $sql = "SELECT x FROM (
                SELECT x FROM (SELECT x FROM a INTERSECT SELECT x FROM b) x
                    UNION
                SELECT x FROM (SELECT x FROM b INTERSECT SELECT x FROM c) x
        ) x ORDER BY x; ";
    $stmt = $db -> query ($sql);
    while ($row = $stmt -> fetch ())
        echo $row [0] . ", ";
    echo "<br>\n";

    $sql = "DROP DATABASE quantity20241125" . session_id () . "; ";
    $db->query ($sql);
}
catch (Exception $e) {
    $sql = "DROP DATABASE quantity20241125" . session_id () . "; ";
    $db->query ($sql);
    session_destroy ();
}

session_destroy ();
?>