Trouble with getting Self Joins to work with SQL project

huangapple go评论59阅读模式
英文:

Trouble with getting Self Joins to work with SQL project

问题

这是您的代码部分的中文翻译:

之前的代码:

$sqlbook = $conn->prepare("SELECT Books.Title, Author.Author, BookLocation.PID, BookLocation.SID from Books LEFT JOIN BookAuthor on Books.BID=BookAuthor.BID LEFT JOIN Author on BookAuthor.AID=Author.AID RIGHT JOIN BookLocation ON Books.BID=BookLocation.BID where Author.Author like '%$search%'");
$sqlbook->execute();
$result = $sqlbook->get_result();
$sqlbook->close();
echo "<br><table class=\"results\">";
echo 
    "<tr>
        <th>书名</th>
        <th>作者</th>
        <th>书架位置 (0 前排, 1 后排)</th>
        <th>书架编号</th>
    </tr>";
while ($row = $result->fetch_assoc()) {
    echo "<tr><td>" . $row["Title"] . "</td><td>" . $row["Author"]  . "</td><td>" . $row["PID"] . "</td><td>" . $row["SID"];
    echo "</td></tr>\n";
}
echo "</table>";

您现在的代码,不起作用:

$sqlbook = $conn->prepare("SELECT Books.Title, Author.Author, Author.ALast as ALast1, Author.AFirst as AFirst1, BookLocation.PID, BookLocation.SID from Books LEFT JOIN BookAuthor on Books.BID=BookAuthor.BID LEFT JOIN Author on BookAuthor.AID=Author.AID JOIN ALast on BookAuthor.AID=ALast1 JOIN AFirst on BookAuthor.AID=AFirst1 RIGHT JOIN BookLocation ON Books.BID=BookLocation.BID where Books.Title like '%$search%'");
$sqlbook->execute();
$result = $sqlbook->get_result();
$sqlbook->close();
echo "<br><table class=\"results\">";
echo 
    "<tr>
        <th>书名</th>
        <th>作者全名</th>
        <th>作者姓氏</th>
        <th>作者名字</th>
        <th>书架位置 (0 前排, 1 后排)</th>
        <th>书架编号</th>
    </tr>";
while ($row = $result->fetch_assoc()) {
    echo "<tr><td>" . $row["Title"] . "</td><td>" . $row["Author"]  . "</td><td>" . $row["ALast"] . "</td><td>" . $row["AFirst"] . "</td><td>" . $row["PID"] . "</td><td>" . $row["SID"];
    echo "</td></tr>\n";
}
echo "</table>";

请注意,这是您提供的代码的翻译部分,不包括其他内容。

英文:

I have a webpage that allows me to display a table from my database that has library info, in this case, author full name, shelf location, book title, etc. I have added first and last name to the database, in the same part as the author name, and I need to be able to display it in the same table with the other info. I am struggling on how to get this to work, and my attempts at self joins have not worked well. The author full name and first and last name parts all use the bookauthor.AID as their primary keys.

This is the code that I had before that works:

$sqlbook = $conn-&gt;prepare(&quot;SELECT Books.Title, Author.Author, BookLocation.PID, BookLocation.SID from Books LEFT JOIN BookAuthor on Books.BID=BookAuthor.BID LEFT JOIN Author on BookAuthor.AID=Author.AID RIGHT JOIN BookLocation ON Books.BID=BookLocation.BID where Author.Author like &#39;%$search%&#39;&quot;);
            /* $sqlbook-&gt;bindParam(&quot;s&quot;,$search); */
            $sqlbook-&gt;execute();
            $result = $sqlbook-&gt;get_result();
            $sqlbook-&gt;close();
            echo &quot;&lt;br&gt;&lt;table class=\&quot;results\&quot;&gt;&quot;;
            echo 
                &quot;&lt;tr&gt;
                    &lt;th&gt;Book Title&lt;/th&gt;
                    &lt;th&gt;Author&lt;/th&gt;
                    &lt;th&gt;Position on Shelf (0 front, 1 back)&lt;/th&gt;
                    &lt;th&gt;Shelf Number&lt;/th&gt;
                &lt;/tr&gt;&quot;;
                while ($row = $result-&gt;fetch_assoc()) {
                    echo &quot;&lt;tr&gt;&lt;td&gt;&quot; . $row[&quot;Title&quot;] . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $row[&quot;Author&quot;]  . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $row[&quot;PID&quot;] . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $row[&quot;SID&quot;];
                    echo &quot;&lt;/td&gt;&lt;/tr&gt;\n&quot;;
                }
                echo &quot;&lt;/table&gt;&quot;;

And this is what I have now that is not currently working:

 $sqlbook = $conn-&gt;prepare(&quot;SELECT Books.Title, Author.Author, Author.ALast as ALast1, Author.AFirst as AFirst1, BookLocation.PID, BookLocation.SID from Books LEFT JOIN BookAuthor on Books.BID=BookAuthor.BID LEFT JOIN Author on BookAuthor.AID=Author.AID JOIN ALast on BookAuthor.AID=ALast1 JOIN AFirst on BookAuthor.AID=AFirst1 RIGHT JOIN BookLocation ON Books.BID=BookLocation.BID where Books.Title like &#39;%$search%&#39;&quot;);
            /* $sqlbook-&gt;bindParam(&quot;s&quot;,&quot;%&quot; . $search . &quot;%&quot;); */
            $sqlbook-&gt;execute();
            $result = $sqlbook-&gt;get_result();
            $sqlbook-&gt;close();
            echo &quot;&lt;br&gt;&lt;table class=\&quot;results\&quot;&gt;&quot;;
            echo 
                &quot;&lt;tr&gt;
                    &lt;th&gt;Book Title&lt;/th&gt;
                    &lt;th&gt;Author Full Name&lt;/th&gt;
                    &lt;th&gt;Author Last Name&lt;/th&gt;
                    &lt;th&gt;Author First Name&lt;/th&gt;
                    &lt;th&gt;Position on Shelf (0 front, 1 back)&lt;/th&gt;
                    &lt;th&gt;Shelf Number&lt;/th&gt;
                &lt;/tr&gt;&quot;;
                while ($row = $result-&gt;fetch_assoc()) {
                    echo &quot;&lt;tr&gt;&lt;td&gt;&quot; . $row[&quot;Title&quot;] . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $row[&quot;Author&quot;]  . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $row[&quot;ALast&quot;] . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $row[&quot;AFirst&quot;] . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $row[&quot;PID&quot;] . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $row[&quot;SID&quot;];
                    echo &quot;&lt;/td&gt;&lt;/tr&gt;\n&quot;;
                }
                echo &quot;&lt;/table&gt;&quot;;

答案1

得分: 0

I've translated the code portion for you:

我猜你是在`Author`表中添加了新的列(`ALast``AFirst`)。在这种情况下,你不需要进行自连接。

SELECT Books.Title
, Author.Author
, Author.ALast
, Author.AFirst
, BookLocation.PID
, BookLocation.SID
FROM Books
  LEFT JOIN BookAuthor ON Books.BID = BookAuthor.BID
  LEFT JOIN Author ON BookAuthor.AID = Author.AID
  RIGHT JOIN BookLocation ON Books.BID = BookLocation.BID
WHERE Books.Title LIKE '%$search%'

Please note that the variable $search is not translated as it appears to be a placeholder in the code.

英文:

I'm guessing you added the new columns (ALast and AFirst) to the Author table. In that case, you don't need a self join.

SELECT Books.Title
, Author.Author
, Author.ALast
, Author.AFirst
, BookLocation.PID
, BookLocation.SID
from Books
  LEFT JOIN BookAuthor on Books.BID=BookAuthor.BID
  LEFT JOIN Author on BookAuthor.AID=Author.AID
  RIGHT JOIN BookLocation ON Books.BID=BookLocation.BID 
where Books.Title like &#39;%$search%&#39;

huangapple
  • 本文由 发表于 2023年4月11日 05:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定