0

我在 stackoverflow 上阅读了无数关于类似问题的线程,但我正在努力解决我的问题并理解各种 MySQL JOIN 查询。我正处于学习 MySQL 的早期阶段,任何帮助将不胜感激。

我有 3 张桌子:

  1. 拍卖
  2. 类别
  3. 很多

拍卖和类别分别具有唯一的主键和拍卖日期、类别标题列。

拍品显示拍品标题、拍品描述等...并使用各自的 ID 链接到拍卖和类别表。

我只是希望显示包含拍卖日期和类别标题的所有行的批次表。

我失败的尝试:

SELECT l.lotid, l.lotnumber, l.lottitle, l.lotdescription, c.categorytitle, a.auctiondate, l.estimatefrom, l.estimateto, l.photo, l.datecreated, l.lastmodified 
FROM lot l
JOIN category c ON l.categoryid = c.categoryid
JOIN auction a ON l.auctionid = a.auctionid
ORDER BY l.lotnumber;

这就像一个 WHERE 查询,正如我确信它应该的那样,省略任何categorytitleauctiondate匹配的行

有简单的解决方法吗?

非常感谢

4

3 回答 3

1

使用LEFT JOIN

SELECT 
    l.lotid, 
    l.lotnumber, 
    l.lottitle, 
    l.lotdescription, 
    IFNULL(c.categorytitle,'n/a'), 
    IFNULL(a.auctiondate,'n/a'), 
    l.estimatefrom, 
    l.estimateto, 
    l.photo, 
    l.datecreated, 
    l.lastmodified 
FROM 
    lot l
LEFT JOIN category c 
    ON l.categoryid = c.categoryid
LEFT JOIN auction a 
    ON l.auctionid = a.auctionid
ORDER BY 
    l.lotnumber;
于 2013-08-08T14:04:41.813 回答
0

为了我的时间和耐心,我简单地编写了很长的代码,输入一个子查询来获得我想要的结果:

<?php
$query = "SELECT l.lotid, l.lotnumber, l.lottitle, l.lotdescription, c.categorytitle, l.estimatefrom, l.estimateto, l.photo, l.datecreated, l.lastmodified 
         FROM lot l
         JOIN category c ON l.categoryid = c.categoryid
         ORDER BY l.lotnumber";

if (!$mysqli->query($query)) {
    echo '<p class="warning">Error executing SELECT query: (' . $mysqli->errno . ') ' . $mysqli->error . "</p>";
}

if ($result = $mysqli->query($query)) {

  while ($row = $result->fetch_row()) {

?>
    <div class="clearfix lot-bg">       
        <p>Lot Number: <span><?php echo $row[1]; ?></span></p>
        <p>Lot Title: <span><?php echo $row[2]; ?></span></p>
        <p>Lot Description: <span><?php echo $row[3]; ?></span></p>
        <p>Category: <span><?php echo $row[4]; ?></span></p>

        <?php

        $subquery = "SELECT a. auctiondate 
                     FROM lot l
                     JOIN auction a ON l.auctionid = a.auctionid
                     WHERE lotid = $row[0]";

        if ($subresult = $mysqli->query($subquery)) {

            if ($stmt = $mysqli->prepare($subquery)) {

            $stmt->execute();
            $stmt->store_result();

            $subrows = $stmt->num_rows;

               for ($j = 0; $j < $subrows; ++$j) {
                    while ($subrow = $subresult->fetch_row()) {
                        echo '<p>Auction Date: <span>' . $subrow[0] . '</span></p>';
                    }
                }
            }
            $stmt->close();
        }
        $subresult->close();

        ?>

        <p>Estimate:  <span>£<?php echo $row[5]; ?> - £<?php echo $row[6]; ?></span></p>
        <p>Date Created: <span><?php echo $row[8]; ?></span></p>
        <p>Last-Modified:  <span><?php echo $row[9]; ?></span></p>
        <figure><a class="fancybox" rel="group" href='img/<?php echo $row[7]; ?>'><img src='img/<?php echo $row[7]; ?>'></a></figure>

        <div class="buttons">           
            <form class="form-signin form-delete" action="lot.php" method="post">
                <input type="hidden" name="delete" value="yes" />
                <input type="hidden" name="lotid" value='<?php echo $row[0]; ?>' />
                <button type="submit" class="btn btn-small btn-primary btn-block">DELETE</button>
            </form>
            <form class="form-signin form-delete" action="lotupdate.php" method="post">
                <input type="hidden" name="edit" value="yes" />
                <input type="hidden" name="lotid" value='<?php echo $row[0]; ?>' />
                <button type="submit" class="btn btn-small btn-primary btn-block">EDIT</button>
            </form>
            <form class="form-signin form-delete" action="lotupdate.php" method="post">
                <input type="hidden" name="photo" value="yes" />
                <input type="hidden" name="lotid" value='<?php echo $row[0]; ?>' />
                <button type="submit" class="btn btn-small btn-primary btn-block">PHOTO</button>
            </form>
        </div>
    </div>

<?php

    }
    $result->close();
}
$mysqli->close();
?>
于 2013-08-08T15:36:34.247 回答
0

您可以简单地使用左连接或右连接来减少行数并获得适当的数据。

这里是教程:LEFT JOIN & RIGHT JOIN

于 2013-08-08T14:07:22.343 回答