我希望该表能够使用右连接从每个主题返回最多2个回复。我可以知道我该怎么做?
<?php
include_once "connect.php";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$form=$_GET;
$trade=$form['tradetype'];
$metal=$form['metal'];
$amount=$form['amount'];
$date=$form['date'];
$stmt = $conn->query("SELECT Discount FROM Contracts WHERE Trade='$trade' AND Metal='$metal' AND Amount='$amount' AND ExpiryDate='$date'");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo ($row['Discount']);
}
?>
答案 0 :(得分:0)
这个前N个查询怎么样:
select tid, rid from(
select t.tid, r.rid,
case when @tid is not null and @tid != t.tid then @rn := 0 else null end reset_rn,
@tid := t.tid tid_change,
@rn := @rn + 1 rn
from topic t
left join reply r on t.tid = r.tid
join (select @rn := 0, @tid := null) rn
order by t.tid, r.rid
) q
where rn < 3;
保留2行的关键是内部查询中的order by
。