如何组合2个具有不同SELECT字段数的SQL查询

时间:2016-10-23 21:35:45

标签: php mysql

通过使用html表单,我想从sql-database中读出数据并在html表中显示它们。所选标准的数量是可变的,有两种类型的选择:

  1. 可以选择应该存在的标准(= $ request1),
  2. 同时

    1. 可以选择不应出现的标准(= $ request2)。
    2. 两个请求都由UNION链接。

      我的代码是:

          if (!empty($daten)) {
              $request1 = "SELECT DISTINCT $selection FROM $tabelle WHERE    
                  $masterarray";
             } else {
               $request1 = "";
             }
      
      
              if ((!empty($daten)) && !empty($contraconcat)) {
              $union1 = "UNION";
             } else {
              $union1 = "";
             }
      
      
            if (!empty($contraconcat)) { 
             $request2 = "SELECT DISTINCT p.Name, absent.Comment
             FROM absent_criteria absent
             JOIN product p
             ON p.master_id = absent.product_id
             GROUP BY product_id
             HAVING SUM(absent_criteria IN ($absentselection))=0";
              } else {
                $request2 = "";
              }
      
      
              $sql = "$request1
              $union1
              $request2";
      
      
              $selection contains the entered selectioncriteria,
              $tabelle the respective tables and
              $masterarray the respective WHERE-conditions
      

      每个可以进行的选择都在数据库中有一个注释字段。 现在,我想在html表中显示结果,第一列包含产品名称。这很好用!

      此外,我希望在附加列中提供相应选择的注释,选择的每个注释都在单独的列中。这就是问题所在。 在request2中,有两个字段的数量不变。但是,在request1中,$ selection的内容变化很大,因为这里的选择是一个动态过程。但是为了将两个SELECT请求与UNION组合,我需要有相同数量的选择。是否有可能为每个选择单独显示注释列?

2 个答案:

答案 0 :(得分:0)

 if (!empty($contraconcat)) { 
   $request2 = "SELECT DISTINCT p.Name as Name, absent.Comment as Comment
   FROM absent_criteria absent
   JOIN product p
   ON p.master_id = absent.product_id
   GROUP BY product_id
   HAVING SUM(absent_criteria IN ($absentselection))=0";
    } else {
      $request2 = "";
    }

if (!empty($daten)) {
    if($request2!=""){
       $request1 = "SELECT DISTINCT $selection, table2.Name,table2.Comment
       FROM $tabelle,($request2) as table2         
       WHERE $masterarray";
       //Here maybe you will need to add validation for both tables because it maybe will have many results 
    }
     $request1 = "SELECT DISTINCT $selection FROM $tabelle WHERE    
        $masterarray";
} else {
     $request1 = "";
   }

您不需要UNION,因为没有相同的数字列。您需要创建表格对象。 首先是p.Name和absent.Comment有一个别名,稍后验证$ request2,如果不为null,则在request1中构建$ request2列,但是看到$ request2有一个别名表。如果$ request2为null,则查询将不会构建

其次,您需要将对象表添加到$ selection,$ tabelle和$ masterarray。如果查询没有构建$ request1和$ request2,没问题,它可以正常工作,如下例所示:

$ selection = t.name,t.product

$ tabelle = table1 t

$ masterarray = t.product LIKE"%R" ...

答案 1 :(得分:0)

在您的情况下,您不需要UNION,UNION用于2选择具有相同数量的列,在您的情况下,请选中此选项,例如:

您的request1,例如,您的查询是:

SELECT c.Color as Color,W.Weight as weight,m.Measure as Measure
FROM color c,weight w,measure m

在这里你必须使用ROW_NUMBER()为结果添加一个临时ID(我不知道数据库使用了什么,但是row_number有效),$ request1将是:

SELECT ROW_NUMBER() OVER(ORDER BY c.Color DESC) as IDReq1,c.Color as Color,W.Weight as weight,m.Measure as Measure
FROM color c,weight w,measure m

例如,$ request1的结果将是:

IDReq1 |颜色|重量|测量

1 |白色| 10 | 10英尺
2 |黑色| 15 | 13英尺.....

稍后,对于请求2,使用与ROW_NUMBER相同的方法,$ request2的结果将是,例如:

IDReq2 |名称|评论
1 |名1 |注释1
2 | NAME2 |注释2 ...

然后,当构建2个查询,JOIN ID各自的查询时,请检查:

SELECT table1.Color,table1.Weight,table1.Measure,table2.Name,table2.Comment
FROM ($request1) as table1, ($request2) as table2
WHERE table1.IDReq1=table2.IDReq2

最终结果将是:

颜色|重量|措施|名称|评论

白色| 10 | 10英尺|名1 |注释1
黑色| 15 | 13英尺| NAME2 |注释2

它可能会对您有所帮助,但这取决于您的验证 祝你好运!