根据第一个表的ID查询多个表

时间:2019-06-20 15:21:10

标签: php mysql datatable subquery

我需要用来自两个不同表的数据填充数据表。 简单,我想,只是加入或子查询。 不幸的是,第二张表不是基于ID的,因此我无法对该表进行过滤。即使可以,我也不知道如何将其放入数据表中。

我已经搜索了好几天了,仍然没有运气。

表wp_mollie_forms_registrations具有:

#id  # description #
#----#-------------#
#100 # Race #
#101 # Pull #
####################

表wp_mollie_forms_registration_fields具有:

#id  # field # value
#----#------#-------#
#100 # Naam # Theun #
#100 # E-mail # test@test.com #
#100 # Leeftijd # 28 #
#100 # Soort voertuig # Auto #
#100 # Betaalmethode # ideal #
#101 # Naam # Theun    #
#101 # E-mail# quest@write.nl #
#101 # Woonplaats # Groningen #
#101 # Merk en type # New Holland #
#101 # Gewichtsklasse # 2.8T #
#101 # Betaalmethode # ideal #
#####################

这是代码:

$query = "select * from A";
$items_result = mysqli_query($conn,$query) or die;

if ($items_result->num_rows > 0) {
echo "<table id='table_id' class='display'><thead><tr><th>ID</th> 
<th>description</th><th>Name</th><th>Age</th><th>Email</th></tr></thead> 
</tbody>";
while ($row = mysqli_fetch_assoc($items_result)){
echo "<tr><td>".$row["id"]."</td><td>".$row["description"]."</td> 
<td>".$Name."</td><td>".$row["Age"]."</td><td>".$row["Email"]."</td>
</tr>";
}

我该怎么做? 从table_A中选择*,然后使用ID选择姓名,年龄和电子邮件,然后将此信息放入我的数据表中,然后转到下一行?

编辑:它有效,但是没有显示Naam(名称)电子邮件和年龄(leeftijd),我现在有:

$query = "SELECT wp_mollie_forms_registrations.id, wp_mollie_forms_registrations.description, tn.value AS 'Naam', te.value AS 'E-mail', ta.value AS 'Leeftijd' ".
"FROM wp_mollie_forms_registrations".
" INNER JOIN (SELECT registration_id, value FROM wp_mollie_forms_registration_fields WHERE field = 'Naam') tn ON wp_mollie_forms_registrations.id = tn.registration_id".
" INNER JOIN (SELECT registration_id, value FROM wp_mollie_forms_registration_fields WHERE field = 'E-mail') te ON wp_mollie_forms_registrations.id = te.registration_id".
" INNER JOIN (SELECT registration_id, value FROM wp_mollie_forms_registration_fields WHERE field = 'Leeftijd') ta ON wp_mollie_forms_registrations.id = ta.registration_id";
if(!mysqli_query($conn, $query)){ echo "Error: ".mysqli_error($conn); }
$items_result = mysqli_query($conn,$query) or die;

if ($items_result->num_rows > 0) {
echo "<table id='table_id' class='display'><thead><tr><th>ID</th><th>description</th><th>Name</th><th>Age</th><th>Email</th></tr></thead></tbody>";
while ($row = mysqli_fetch_assoc($items_result)){
    echo "<tr><td>".$row["id"]."</td><td>".$row["description"]."</td><td>".$row["tn.value"]."</td><td>".$row["ta.value"]."</td><td>".$row["te.value"]."</td>
</tr>";
}
    echo "</tbody></table>";
} else {
    echo "0 results";
}

1 个答案:

答案 0 :(得分:0)

以下是应根据您的回复工作的代码:

$query = "SELECT wp_mollie_forms_registrations.id as 'ID', wp_mollie_forms_registrations.description as 'Description', tn.value AS 'Naam', te.value AS 'Email', ta.value AS 'Leeftijd' ".
"FROM wp_mollie_forms_registrations".
" INNER JOIN (SELECT registration_id, value FROM wp_mollie_forms_registration_fields WHERE field = 'Naam') tn ON wp_mollie_forms_registrations.id = tn.registration_id".
" INNER JOIN (SELECT registration_id, value FROM wp_mollie_forms_registration_fields WHERE field = 'E-mail') te ON wp_mollie_forms_registrations.id = te.registration_id".
" INNER JOIN (SELECT registration_id, value FROM wp_mollie_forms_registration_fields WHERE field = 'Leeftijd') ta ON wp_mollie_forms_registrations.id = ta.registration_id";
if(!mysqli_query($conn, $query)){ echo "Error: ".mysqli_error($conn); }
$items_result = mysqli_query($conn,$query) or die;

if ($items_result->num_rows > 0) {
echo "<table id='table_id' class='display'><thead><tr><th>ID</th><th>description</th><th>Name</th><th>Age</th><th>Email</th></tr></thead></tbody>";
while ($row = mysqli_fetch_assoc($items_result)){
    echo "<tr><td>".$row["ID"]."</td><td>".$row["Description"]."</td><td>".$row["Naam"]."</td><td>".$row["Leeftijd"]."</td><td>".$row["Email"]."</td>
</tr>";
}
    echo "</tbody></table>";
} else {
    echo "0 results";
}

之所以没有显示某些列的原因是因为您在PHP的'echo'语句中具有错误的值。例如,tn.value as Naamtn.value定义为“ Naam”。因此,更改PHP中的值以反映该值可使查询工作。

有关查询操作的详细说明,这是一个超简化的版本。假设您必须使用表A和B,并且每个表都有一个字段“ id”和“ value”。看一下这个查询:

SELECT A.value, B.value
FROM A
INNER JOIN B
ON A.id = B.id

这将从表A和B中选择值并返回它们,但是仅当A中的ID与B中的ID之间存在匹配时才可以。您可以阅读有关联接Here的更多信息。

子查询部分相对简单。它会从表B中选择所有“ registration_id”和“值”,其中该字段是特定类型,例如“ Naam”。