通过使用html表单,我想从sql数据库中读出数据并在html表中显示它们。所选标准的数量是可变的,有两种类型的选择:
- 可以选择应该存在的标准(= $request1),
和
- 可以选择不应该存在的标准(= $request2)。
两个请求通过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的内容是高度可变的,因为这里的选择是一个动态过程。但是要用UNION组合两个select请求,我需要有相同数量的选择。是否有可能为每个选择分别显示评论列?
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,因为没有相同数量的列。您需要为表创建对象。姓名和缺席。注释有别名,稍后验证$request2,如果不为空,然后在request1中建立$request2列,但看到$request2有别名表。如果$request2为null,则查询将不会建立
第二,你需要在$selection,$tabelle和$masterarray中添加一个对象表。如果query不构建$request1和$request2,没有问题,它可以工作,如下例所示:
选择美元= t.name t.product
$ table = table1 t
$masterarray= t.product LIKE "%R"…
在你的情况下,你不需要UNION, UNION是2选择相同数量的列,在你的情况下,检查这个,例如:
您的request1,例如,您的查询是:
SELECT c.Color as Color,W.Weight as weight,m.Measure as Measure
FROM color c,weight w,measure m
这里你必须添加一个临时ID到结果与ROW_NUMBER()(我不知道什么数据库使用,但与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,,,,,| name1 | comment1
,,,,2,,,,,| name2 | comment2…
然后,当构建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英尺,,,,| name1 | comment1
15 |黑色,,,,,,,,,|,,13 ft ,,,| name2 | comment2
它可能会帮助你,但这取决于你的验证好运!