PHP,MySQL UNION查询错误

时间:2013-03-25 03:27:36

标签: php mysql ajax

我在尝试使用PHP / MySQl-AJAX调试此查询时遇到问题: 变量$ param是表单文本框上的AJAX调用的结果。本质上,我试图在三个数据库表上生成动态搜索,不幸的是这些数据库具有不同的字段(因此是concat)。数据是具有使用不同方法生成的空间位置(对于前两个表)的作业的地址,最后一个表是非空间数据。

            $fetch = "(SELECT JobNo AS JobNo, CONCAT(Title1, '-', Title2, '-', Title3) AS Description, 'Hurricane' as type FROM Hurricanev2 WHERE Title1 REGEXP '$param' OR Title2 REGEXP '$param' OR Title3 REGEXP '$param') 
       UNION ALL
       (SELECT jobNo AS JobNo, description As Description, address As Geocoded_address, 'geocoded' as type FROM jr WHERE description REGEXP '$param' OR address REGEXP '$param')
       UNION ALL
       (SELECT job As JobNo, description As Description, 'plan' as type FROM register WHERE description REGEXP '$param')";


    while ( $row = mysql_fetch_object( $fetch ) ) {

        $sResults .= '<tr>';
        $sResults .= '<td>' . $row['JobNo'] . '</td>';
        $sResults .= '<td>' . $row['Description'] . '</td></tr>';
    }

提前致谢

2 个答案:

答案 0 :(得分:1)

您无法合并具有不同列数的结果集。

尝试以下方法;

$fetch = "(SELECT JobNo AS JobNo, CONCAT(Title1, '-', Title2, '-', Title3) AS Description, 'Hurricane' as type, 'extra' FROM Hurricanev2 WHERE Title1 REGEXP '$param' OR Title2 REGEXP '$param' OR Title3 REGEXP '$param') 
   UNION ALL
   (SELECT jobNo AS JobNo, description As Description, address As Geocoded_address, 'geocoded' as type FROM jr WHERE description REGEXP '$param' OR address REGEXP '$param')
   UNION ALL
   (SELECT job As JobNo, description As Description, 'plan' as type, 'extra' FROM register WHERE description REGEXP '$param')";

答案 1 :(得分:0)

对于UNION,每个SELECT需要具有相同数量的列。尝试在第一个和最后一个SELECT上添加一个额外的null,以便在out中。