SQL连接第三个表

时间:2016-12-17 23:00:29

标签: mysql inner-join

所以我有一个三表设置,如下所示......

表1 - 收到项目

ri_id | ri_name | ri_reciept 
----------------------------
1     | nails   | 1
2     | screws  | 1
3     | hammer  | 1

表2 - 收据

r_id | r_name   | r_mop
-----------------------
1    | reciept1 | 1
2    | reciept2 | 2
3    | reciept3 | 2

表3 - method_of_payment

m_id | m_name
-------------
1    | Bank Transfer
2    | Cash
3    | Cheque

这是我尝试运行的SQL代码

SELECT * FROM reciept_items ri 
INNER JOIN reciepts r ON ri.ri_reciept = r.r_id 
INNER JOIN method_of_payment mop ON r.r_mop = mop.m_id 
WHERE ri_reciept = :id

更新:最后WHERE行的作用如下......

$queryRecieptItems = $dbh->prepare("
                SELECT * FROM reciept_items ri 
                INNER JOIN reciepts r ON ri.ri_reciept = r.r_id 
                INNER JOIN method_of_payment mop ON r.r_mop = mop.m_id 
                WHERE ri.ri_reciept = :id
            ");
            $queryRecieptItems->execute(array( "id"=>$reciept['r_id'] ));

我试图访问' m_name'在循环表1的同时,任何想法为什么上述都不起作用?

1 个答案:

答案 0 :(得分:1)

您需要在where子句中使用表名称前缀$.each(page_name, function(i, p) { $.each(extensions, function(ii, e) { $.each(query, function(iii, q) { var data = p + "." + e + "?" + q; dorks.push(data); $('#result').append(data + '\n'); $("#amt").text(dorks.length); }); }); }); 列(尽管它应该没有前缀)并传递正确的id代替“:id”,例如以下应该有效:

ri_reciept

以下是 SQL Fiddle