简体   繁体   English

PHP,MySQL UNION查询错误

[英]PHP, MySQL UNION Query Error

I am having a problem trying to debug this query using PHP/MySQl-AJAX: The variable $param is the result of a AJAX call on a form textbox. 我在尝试使用PHP / MySQl-AJAX调试此查询时遇到问题:变量$ param是表单文本框上AJAX调用的结果。 In essence I am trying to generate a dynamic search over three database tables which unfortunately have different fields (hence the concat). 本质上,我试图对三个数据库表(它们不幸地具有不同的字段)进行动态搜索(因此称为concat)。 The data are addresses of jobs which have a spatial location (for the first two tables) generated using different methods, the last table is non spatial data. 数据是具有使用不同方法生成的空间位置(对于前两个表)的作业的地址,最后一个表是非空间数据。

            $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>';
    }

thanks in advance 提前致谢

You can't union the result sets which has different number of columns. 您不能合并具有不同列数的结果集。

Try the following; 尝试以下方法;

$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')";

for UNION you need to have the same number of columns for each SELECT. 对于UNION,每个SELECT的列数必须相同。 Try adding in an extra null on first and last SELECT to even in out. 尝试在第一个和最后一个SELECT上添加一个额外的null以使输出均匀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM