Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql php JOIN语句只返回第二个表中的一行_Mysql_Sql_Join - Fatal编程技术网

Mysql php JOIN语句只返回第二个表中的一行

Mysql php JOIN语句只返回第二个表中的一行,mysql,sql,join,Mysql,Sql,Join,我有一个php语句: $takeBL = $connection -> prepare("SELECT bestelldatabase.*, zurückschreibendatas.* FROM bestelldatabase LEFT JOIN zurückschreibendatas USING (lieferscheinid) WHERE bestelldatabase.lieferscheinid = ?"); $takeBL -> execute([$_GET['id']

我有一个php语句:

$takeBL = $connection -> prepare("SELECT bestelldatabase.*, zurückschreibendatas.* FROM bestelldatabase LEFT JOIN zurückschreibendatas USING (lieferscheinid) WHERE bestelldatabase.lieferscheinid = ?");
$takeBL -> execute([$_GET['id']]);
while ($getAllBL = $takeBL -> fetch()) {

    array_push($products, $getAllBL);
}
这一切都是可行的,但唯一的问题是,sql只返回联接表中的第一个匹配行,然后将它们推送到数组中。 我不太懂join语句,当我试图用谷歌搜索答案时,我真的很困惑

这些都是从“bestelldatabase”到“lieferscheinid”37的行:

在这里,您可以看到“zurückschreibendatas”和“lieferscheinid”37中的所有行:

为了更好地理解,这里是>var_dump($products):

为了更好地理解,还有另一种说法:

array(22) { 
["id"]=> string(3) "150" [0]=> string(3) "142" 
["lieferscheinid"]=> string(2) "37" [1]=> string(2) "37" 
["warenid"]=> string(1) "7" [2]=> string(1) "7" 
["anzahl"]=> string(1) "6" [3]=> string(1) "6" 
["flaschen"]=> string(1) "0" [4]=> string(1) "0" 
[5]=> string(3) "150" 
[6]=> string(2) "37" 
["bestellid"]=> string(3) "140" [7]=> string(3) "140" 
["gesamtkisten"]=> string(2) "15" [8]=> string(2) "15" 
["gesamtflaschen"]=> string(1) "0" [9]=> string(1) "0" 
["zurückkisten"]=> string(1) "6" [10]=> string(1) "6" 
["zurückflaschen"]=> string(1) "0" [11]=> string(1) "0" 
} 
编辑 这是我编辑的声明:

"SELECT 
b.id, 
b.lieferscheinid, 
b.warenid, 
b.anzahl, 
b.flaschen, 
z.id, 
z.lieferscheinid, 
z.bestellid, 
z.gesamtkisten, 
z.gesamtflaschen, 
z.zurückkisten, 
z.zurückflaschen 
FROM bestelldatabase AS b 
LEFT JOIN zurückschreibendatas AS z 
USING (lieferscheinid) 
WHERE b.lieferscheinid = ?"
我还忘了提到一件重要的事情:如果我把WHERE子句从“WHERE b.lieferscheinid=?”改为“WHERE z.lieferscheinid=?”

问题转过来,然后是第一个表“bestelldatabase”,它只返回一行。
那么我做错了什么呢?

您需要在语句上使用
,并根据相关列将两个表连接起来,在本例中是
bestelldatabase.id
zurückschreibendatas.bestellid

因此,整个查询结果如下:

SELECT bestelldatabase.*, zurückschreibendatas.* 
FROM bestelldatabase 
LEFT JOIN zurückschreibendatas 
on bestelldatabase.id = zurückschreibendatas.bestellid 
WHERE bestelldatabase.lieferscheinid = ?
其中
限制返回的数据,而
上的
告诉联接应该如何映射

SELECT bestelldatabase.*, zurückschreibendatas.* 
FROM bestelldatabase 
LEFT JOIN zurückschreibendatas 
on bestelldatabase.id = zurückschreibendatas.bestellid 
WHERE bestelldatabase.lieferscheinid = ?