简体   繁体   English

PHP MySQL-左联接3表

[英]PHP MySQL - Left Join 3 Tables

i have a logic problem in my PHP/SQL Code. 我的PHP / SQL代码中存在逻辑问题。

My script delivers a list of titles. 我的脚本提供了标题列表。 You can view a single title by clicking on it (ajax). 您可以通过单击来查看单个标题(ajax)。 In this process, the list is saving in the database. 在此过程中,列表将保存在数据库中。

If you click "back" you should see the previous list again. 如果单击“返回”,您应该再次看到上一个列表。 And here is my problem... 这是我的问题

Table ca_begriff 表ca_begriff

id  title
2   Giraffe
3   Wetterhahn
4   Eiswürfel
5   Toaster

Table ca_history 表ca_history

id  date
46  1452592732
45  1452592731
44  1452592662

Table ca_history_begriffe 表ca_history_begriffe

id      history begriff position
263     46      3       1
264     46      9       2
265     46      10      3
266     46      2       4
267     46      4       5

The Problem must be here: 问题必须在这里:

$sql = "SELECT begriffe.id, begriffe.title FROM ca_begriffe
        LEFT JOIN ca_history_begriffe ON ca_begriffe.id = ca_history_begriffe.begriff
        LEFT JOIN ca_history ON ca_history_begriffe.history = ca_history.id
        WHERE ca_history.id = ".$_GET['r']."
        ORDER BY ca_history_begriffe.position";

$result = $conn->query($sql);

phpMyAdmin的

Thank you 谢谢

Greets from Germany 来自德国的问候

I think you specified a wrong column names 我认为您指定了错误的列名

$sql = "SELECT ca_begriffe.id, ca_begriffe.title FROM ca_begriffe
    LEFT JOIN ca_history_begriffe ON ca_begriffe.id = ca_history_begriffe.begriff
    LEFT JOIN ca_history ON ca_history_begriffe.history = ca_history.id
    WHERE ca_history.id = ".$_GET['r']."
    ORDER BY ca_history_begriffe.position";

Ok, the error is: "non-object", but you didn't post that code. 好的,错误是:“非对象”,但您没有发布该代码。 A right use below: 正确的用法如下:

$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {
   // $row is filled!
}

Docs: http://php.net/manual/en/mysqli-result.fetch-assoc.php 文件: http//php.net/manual/en/mysqli-result.fetch-assoc.php

SOLVED! 解决了!

ca_begriffe.id, ca_begriffe.title

sorry. 抱歉。 that was stupid. 那真是愚蠢。 I forgot that "ca_". 我忘了那个“ ca_”。 It has been years ago since my last SQL project. 自从我上一个SQL项目以来已经有好几年了。 Thank you guys! 感谢大伙们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM