使用JOIN时如何检测来自哪个表的行

时间:2016-09-18 11:19:18

标签: php mysql

我有这个mysql查询:

SELECT  R.*, A.*

    FROM             buy A
    JOIN             return R
    ON               A.id_buyer = R.id_buyer 
    WHERE            A.id_acheter = '$id' and R.stats in (5, 4) and A.stats in (14, 15) 
    ORDER BY R.id, A.id

首先,当我使用此查询时,我得到一个重复的行...我使用GROUP BY来解决问题,但还没有完成。

这里有样本数据:

购买表:

> +-------------+---------------+-----------+-------------+-------------+
> |id           |  id_buyer     | product   | title       | stats       |
> +-------------+---------------+-----------+-------------+-------------+
> |1            | 12            | Amazon    | Shoes       | 4           |
> +-------------+---------------+-----------+-------------+-------------+
> |2            | 12            | Amazon    | Glasses     | 5           |
> +-------------+---------------+-----------+-------------+-------------+
> |3            | 16            | Ebay      | Glasses     | 2           |
> +-------------+---------------+-----------+-------------+-------------+

RETURN表:

> +-------------+---------------+-----------+-------------+-------------+
> |id           |  id_buyer     | Message   | Date        | stats       |
> +-------------+---------------+-----------+-------------+-------------+
> |1            | 12            | Hi        | 02/05/16    | 14          |
> +-------------+---------------+-----------+-------------+-------------+
> |2            | 12            | What?     | 02/05/16    | 0           |
> +-------------+---------------+-----------+-------------+-------------+
> |3            | 12            | Yes       | 02/05/16    | 15          |
> +-------------+---------------+-----------+-------------+-------------+

期望的结果:

由于我在表格中没有相同的列,我不知道如何管理我的PHP代码以显示此结果?

> +-------------+---------------+-----------+-------------+-------------+
> |1            | 12            | Hi        | 02/05/16    | 14          |
> +-------------+---------------+-----------+-------------+-------------+

> +-------------+---------------+-----------+-------------+-------------+
> |3            | 12            | Yes       | 02/05/16    | 15          |
> +-------------+---------------+-----------+-------------+-------------+

> +-------------+---------------+-----------+-------------+-------------+
> |1            | 12            | Amazon    | Shoes       | 4           |
> +-------------+---------------+-----------+-------------+-------------+

> +-------------+---------------+-----------+-------------+-------------+
> |2            | 12            | Amazon    | Glasses     | 5           |
> +-------------+---------------+-----------+-------------+-------------+

这里我做了但没有成功:

<table>
<?php
$id = "12";
$req = $mysqli->query("SELECT  R.*, A.*, R.id as id_reservation, A.id as id_buy

    FROM             buy A
    JOIN             return R
    ON               A.id_buyer = R.id_buyer 
    WHERE            A.id_acheter = '$id' and R.stats in (5, 4) and A.stats in (14, 15) 
    ORDER BY R.id, A.id");

While($rows = mysqli_fetch_array($req)) {

$id_buyer = $rows["id_buyer"];
if(isset($rows["id_reservation"])) {
$id = $rows["id_reservation"];
$result1 = $rows["product"];
$result2 = $rows["title"];
} else {
$id = $rows["id_buy"];
$result1 = $rows["message"];
$result2 = $rows["date"];
}
$stats = $rows["stats"];
?>
<tr>
<td><?=$id?></td>
<td><?=$id_buyer?></td>
<td><?=$result1?></td>
<td><?=$result2?></td>
<td><?=$stats?></td>
</tr>
<?php
}
?>
</table>

编辑(UNION approch):

SELECT  
     R.'from return'
   , R.`id` 
   , R.`id_buyer` 
   , null as product
   , null as title
   , R.`id_seller` as id_seller
   , R-`message` 
   , R.`stats` 
   , R.`date` 
   , R.`update`
FROM    return R
WHERE R.id_buyer = '$user'
AND (R.stats='1' OR R.stats='3')
UNION ALL 

SELECT  
     A.'from buy'
   , A.`id` 
   , A.`id_buyer` 
   , A.`product` 
   , A.`title` 
   , null 
   , null
   , A.`stats` 
   , A.`date` 
   , null
FROM    buy A
WHERE 
A.id_buyer = '$user'
AND A.stats='4'
ORDER BY R.date DESC LIMIT $from , 20

此处我们有UNION个approch,但只有第一个SELECT的列有效,第二个SELECT列不起作用。

我仍然只为第二个选择列收到此错误:Notice: Undefined index: product in ...

0 个答案:

没有答案