0

我有这个功能:

function view_user_anunt($user) {
    $query="SELECT * FROM `anunturi`
            FULL OUTER JOIN tranzactie
            ON anunturi.tranzactie = tranzactie.id_tranzactie WHERE `anunturi.user`=:code";

        $stmt = $this->dbh->prepare($query);
        $stmt->bindParam(':code', $user, PDO::PARAM_INT);
        $stmt->execute();
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $result) 
        {
            $view[]="
            <tr>
            <td>".$result['id_anunt']."</td>
            <td>".$result['den_tranzactie']."</td>
            <td>".$result['den_proprietate']."</td>
            <td><a href='#' id='vizualizare'>Select</a></td>
            <td><a href='#' id='modificare'>Select</a></td>
            </tr>";

        }
    return $view;


}

和3张桌子:

阿努图里

  • id_anunt (int) 自动递增
  • tranzactie(int)
  • 提示(整数)
  • 用户(整数)

转瞬即逝

  • id_tranzactie (int) 自动递增
  • den_tranzactie varchar

提示

  • id_proprietate (int) 自动递增
  • den_proprietate varchar

我需要一个好的查询或想法来从 anunturi 中获取每行的事务名称 (den_tranzactie) 和专有名称 den_proprietate,其中 anunturi.user = $user。

提前致谢...

4

2 回答 2

0

像这样的东西?

'
SELECT t.den_tranzactie, tip.den_proprietate 
FROM
anunturi a
JOIN tranzactie t ON a.tranzactie = t.id_tranzactie 
JOIN tip ON a.tip = tip.id_proprietate 
WHERE a.user =:code
于 2013-10-09T07:31:57.220 回答
0

试试这个 sql 查询

 $query="SELECT t1.*, t2.*, t3.*
         FROM table1 t1 
         LEFT JOIN table2 t2 ON t1.id = t2.foreightkey_id
         LEFT JOIN table3 t3 ON t1.id = t3.foreightkey_id
         WHERE t1.user=:code
";

您可以使用 INNER JOIN 仅接收在所有表中都有数据的行。例如

$query="SELECT t1.*, t2.*, t3.*
             FROM table1 t1 
             INNER JOIN table2 t2 ON t1.id = t2.foreightkey_id
             INNER JOIN table3 t3 ON t1.id = t3.foreightkey_id
             WHERE t1.user=:code
    ";

或者,如果您想从已连接的表 1 和表 2 中获取所有数据,而仅从表 3 中获取已连接的数据

$query="SELECT t1.*, t2.*, t3.*
                 FROM table1 t1 
                 INNER JOIN table2 t2 ON t1.id = t2.foreightkey_id
                 LEFT JOIN table3 t3 ON t1.id = t3.foreightkey_id
                 WHERE t1.user=:code
        ";
于 2013-10-09T07:44:39.107 回答