我已经在stackoverflow上阅读了与此类似的事情的无数线程,但我正在努力解决我的问题并理解各种MySQL JOIN查询。我正处于学习MySQL的早期阶段,任何帮助都会受到赞赏。
我有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查询一样,因为我确定应该这样做,省略categorytitle
和auctiondate
不匹配的任何行
有简单的解决方法吗?
非常感谢
答案 0 :(得分: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;
答案 1 :(得分:0)
您可以简单地使用左连接或右连接来减少行数,并获得适当的数据。
以下是教程:LEFT JOIN& RIGHT JOIN
答案 2 :(得分: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();
?>