gpt4 book ai didi

php - 如何从两个表中获取匹配和不匹配的数据?

转载 作者:行者123 更新时间:2023-11-29 21:03:00 24 4
gpt4 key购买 nike

我读过,当我使用 LEFT JOIN 时,

Each item in the left table will show up in a MySQL result, even if there isn't a match with the other table that it is being joined to.

这就是我计划在查询中使用的内容,因为我想查找 alumni.nameprofile.name 的匹配项,并且仍然结果来自alumni 表,并且仅匹配 profile 表中的记录。这就是我对 LEFT JOIN 的理解。我理解正确吗?如果我没有,请告诉我。

如果我是正确的,那么我还有另一个问题。因为我希望当 alumni.nameprofile.name 不匹配时,仍然会打印两个表中的记录。

类似这样的事情

if alumni.name==profile.name
output alumni.name
else if alumni.name != profile.name
output alumni.name and profile.name

这只是我想要如何做到这一点的一个示例。不要评价我的情况,我知道这不是正确的方法,但我的想法就是这样。希望你能帮助我。

示例片段:

<!DOCTYPE html>
<html>
<body>

<table width="30%" align="center">
<th> Alumni ID </th>
<th> Alumni Firstname </th>
<th> Alumni Lastname </th>

<tr align="center">
<td> 1 </td>
<td>Jill</td>
<td>Smith</td>
</tr>

<tr align="center">
<td> 2 </td>
<td>John</td>
<td>Doe</td>
</tr>
<tr align="center">
<td> 3 </td>
<td>Ed</td>
<td>Fin</td>
</tr>

</table>
<br><br>

<table width="30%" align="center">
<th> Profile ID </th>
<th> Profile Firstname </th>
<th> Profile Lastname </th>

<tr align="center">
<td> 1 </td>
<td>Ed</td>
<td>Fin</td>
</tr>

<tr align="center">
<td> 2 </td>
<td>Ricky</td>
<td>Perez</td>

<tr align="center">
<td> 3 </td>
<td>Jill</td>
<td>Smith</td>
</tr>

</table>

<table width="30%" align="center">
<center><h4>EXPECTED OUTPUT </h4></center>
<th> ID </th>
<th> Firstname </th>
<th> Lastname </th>

<tr align="center">
<td> 1 (from table alumni)</td>
<td>Jill </td>
<td>Smith </td>
</tr>
<tr align="center">
<td> 2 (from tbl alumni)</td>
<td>John</td>
<td>Doe</td>
</tr>
<tr align="center">
<td> 2 (from tbl profile)</td>
<td>Ricky</td>
<td>Perez</td>
</tr>
<tr align="center">
<td> 3 (from tbl alumni)</td>
<td>Ed</td>
<td>Fin</td>
</tr>

</table>


</body>
</html>

最佳答案

SELECT IF(alumni.name=profile.name, alumni.name, CONCAT(alumni.name, ' ', profile.name)) AS 名称来自校友LEFT JOIN profile ON alumni.indexkey = profile.fkey

关于php - 如何从两个表中获取匹配和不匹配的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37050953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com