MYSQL脚本,用于查询2个表并获取门牌号和邮政编码的匹配项

MYSQL脚本,用于查询2个表并获取门牌号和邮政编码的匹配项,mysql,Mysql,这里有点mysql noob我需要一个脚本,我将与cron jobs一起每晚运行一次,以比较两个表,并根据“House_number”和“postcode”列查找匹配项 将识别匹配项,然后生成文本文件。 理想情况下,我希望通过sendmail或同等方式将此邮件发送给我 这两个表被命名为 用户表 商务桌 提前感谢因为你是一个noob,需要一些帮助,我为你准备了这个,请注意,这还没有经过测试,在优化和可重用性方面非常粗糙,但类似的东西,将由你来测试:) 请您向我们展示一下您的尝试和迄今为止的成果。

这里有点mysql noob我需要一个脚本,我将与cron jobs一起每晚运行一次,以比较两个表,并根据“House_number”和“postcode”列查找匹配项 将识别匹配项,然后生成文本文件。 理想情况下,我希望通过sendmail或同等方式将此邮件发送给我

这两个表被命名为 用户表 商务桌


提前感谢

因为你是一个noob,需要一些帮助,我为你准备了这个,请注意,这还没有经过测试,在优化和可重用性方面非常粗糙,但类似的东西,将由你来测试:)



请您向我们展示一下您的尝试和迄今为止的成果。还有一个表模式也会有帮助。嗨,Ryan,我还没有设置好这个,但我会。我只需要一些初步的帮助。我将有两个表列“postcode”和“house_number”。我需要一个脚本来比较它们,根据这两个列查找匹配项。当你说比较时,你是什么意思。一个门牌号可以是42,一个邮政编码可以是7800基本上我需要知道你到底想要实现什么。因为比较两个表可以通过一个连接或两个查询来完成。嗨,Ryan,谢谢你的耐心,是的,让我们假设在user_表和business_表上,我们有一个条目,其中“house_number”是42,poscode是7800。但我还有另外两个匹配项,分别是“house_number”=43和postcode=7801。我需要它删除所有匹配项并输出到一个txt文件。Ryan为此感谢万分。我将对此进行测试,希望能让它正常工作。你是一个明星,谢谢,没有问题,任何时候:)
<?php 
    //store this in a seperate file or leave it here
    $databaseDetails = array(
        'host' => 'localhost', 
        'dbName' => 'databaseName',
        'username' => 'user',
        'password' => 'password'
    );

    //instantiate our final array
    $houseArr = array();

    try {

        //connection using PDO (very secure)
        $conn = new PDO('mysql:host='.$databaseDetails['host'].';dbname='.$databaseDetails['dbname'], $databaseDetails['username'], $databaseDetails['password']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        //This commented out section is to show you how to prepare a query
        /*
        $prepare = $conn->prepare('SELECT house_number, post_code FROM user_table WHERE created = :date');
        $prepare->execute(array(
            'date' => date('Y-m-d H:i:s', strtotime('Now'))
        ));
        */

        //query for users
        $data = $conn->query('SELECT id, house_number, post_code /*Additional fields here*/ FROM user_table');

        //this will create an array of data that i will explain below
        foreach($data as $row) {
            $houseArr[$row['house_number']]['users'][$row['id']] = $row['post_code'];   
        }

        //query for business
        $data = $conn->query('SELECT id, house_number, post_code /*Additional fields here*/ FROM business_table');

        //this will create an array of data that i will explain below
        foreach($data as $row) {
            $houseArr[$row['house_number']]['business'][$row['id']] = $row['post_code'];    
        }

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
        exit();
    }

    if(!empty($houseArr)) {

        /*
         *  the houseArr will have house_number as an index, then it will have multiple entries under either users or business
         * for example 
         * user house 43 and postcode 7800 with database id 1900
         * user house 43 and postcode 7801 with database id 1950
         * business house 43 and postcode 7800 with database id 2000
         * business house 43 and postcode 7801 with database id 2050
         * user house 42 and postcode 5400 with database id 2100
         * business house 42 and postcode 7600 with database id 2200
         *   
         * the array will spit out 
         * array(
         *      43 =>                       This is where the house number will be
         *          'user' =>               This is where all the users with house number x will be
         *              '1900' => '7800',   This is a key pair value of the id of the user and the post code they entered
         *              '1910' => '7801'
         *          'business' =>           This is where all the businesses will be with house number x 
         *              '2000' => '7800',   This is a key pair value of the id of the business and the post code they entered
         *              '2050' => '7801'
         *      42 => 
         *          'user' => 
         *              '2100' => 5400,
         *          'business' => 
         *              '2200' => 7600
         * )
         * 
         */

        //this will write the txt file
        file_put_contents('address.txt', print_r($houseArr, true)); 
    }
?>