MySQL20241227Quantity.php


<?php
session_start ();

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

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

$sql = "CREATE DATABASE mysql20241227" . session_id () . "; ";
$db->query ($sql);

try {
    $sql = "USE mysql20241227" . 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 () % 32 . "); ";
        $sql .= "INSERT INTO b (x) VALUES (" . rand () % 18 . "); ";
        $sql .= "INSERT INTO c (x) VALUES (" . rand () % 64 . "); ";
        $db->query ($sql);
    }

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


    $sql = "DROP DATABASE mysql20241227" . session_id () . "; ";
    $db->query ($sql);
}
catch (Exception $e) {
    $sql = "DROP DATABASE mysql20241227" . session_id () . "; ";
    echo "Error!";
    $db->query ($sql);
}


session_destroy ();
?>