我目前有一张表需要显示如下:
但正如你所看到的那样,它没有按照"数量下降" (在代码中命名为$ amount和amount) 我首先需要从表中收集名称和drop_id。这是这样做的:
//----------------FETCH ALL CONTENTS FROM DROPTABLE TO DISPLAY DROPS------------\\
$query = "SELECT * FROM droptable
WHERE boss_id = ". $boss ."";
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row){
$drop_id = $row['drop_id'];
$drop = $row['dropname'];
$boss_id = $row['boss_id'];
$picture = $row['picture'];
这将获取drop(dropname)的名称。它的id(drop_id),boss id(boss_id)和它的图片(picture)。 然后继续检查我是否从另一张桌子上记下了老板的一滴:
//----------------FETCH ALL LOGGED DROPS FOR DISPLAYING AMOUNT------------\\
$query1 = "SELECT * FROM dropcounter
WHERE boss_id = ". $boss ." AND userid = ". $user ." AND drop_id = ". $drop_id ."";
$stmt1 = $pdo->prepare($query1);
$stmt1->execute();
$result1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
echo "<tr>";
echo "<td><img src='../images/". $picture ."'</td>";
echo "<td>". $drop . "</td>";
echo "<td>". $amount ."</td>";
echo "<td>". $percent ."</td>";
echo "<form action='logger.php' method='post'>";
echo "<td><input type='hidden' value=". $drop_id ." name='drop_id'>";
echo "<input type='hidden' value=". $boss_id ." name='boss_id'>";
echo "<input type='hidden' value=". $user ." name='user'>";
echo "<input type='submit' value='Add'></td>";
echo "</tr>";
echo "</form>";
这就是我想要的所有内容排序量下降或下降百分比(因为他们将排序相同)。香港专业教育学院尝试添加&#34; ORDER BY金额DESC&#34;在第二个查询但它没有排序。
我也试过使用JOIN,但它没有接近我想要的结果,我在查询中被困3天,所以转而使用上面的代码。但如果我希望的结果能够完成,我愿意再次使用加入。
这里的JOIN代码不起作用:
$query = "SELECT dropcounter.drop_id, dropcounter.boss_id, dropcounter.add_date, dropcounter.username, dropcounter.amount, droptable.drop_id, droptable.dropname, droptable.boss_id, droptable.wiki_link, droptable.picture
FROM droptable
JOIN dropcounter
ON droptable.boss_id = dropcounter.boss_id
WHERE dropcounter.drop_id = droptable.drop_id AND droptable.boss_id = ". $boss ." AND dropcounter.username = ". $user ."
ORDER BY dropcounter.amount";
这是我桌子上的结构: DROPCOUNTER TABLE:
以及这里的DROPTABLE表:
如果有人能帮助我,如果有人,我会很高兴你的善意!
如果有人想尝试一下,这里有一个小提琴。我已导入一些示例数据:http://sqlfiddle.com/#!2/d66846/1/0答案 0 :(得分:1)
如果您希望获得LEFT JOIN
中不匹配droptable
的行,则需要使用dropcounter
。所有引用dropcounter
的测试都必须在ON
子句中,否则空匹配将导致测试失败,并且这些行将被过滤掉。
SELECT dc.add_date, dc.username, IFNULL(dc.amount, 0) amount, dt.drop_id, dt.dropname, dt.boss_id, dt.wiki_link, dt.picture
FROM droptable dt
LEFT JOIN dropcounter dc
ON dt.boss_id = dc.boss_id AND dc.drop_id = dt.drop_id AND dc.userid = $user
WHERE dt.boss_id = $boss
ORDER BY amount
答案 1 :(得分:0)
你正试图在联合声明中使用:
dropcounter.username = ". $user ."
包含2个错误: 1.该表中没有字段用户名 2.您没有在&#39;&#39;
中附上字符串将其更改为dropcounter.userid
此外,您不需要选择droptable.drop_id和droptable.boss_id,因为它已从dropcounter表中选择。
编辑:基于你的小提琴:
SELECT dropcounter.drop_id, dropcounter.boss_id, dropcounter.userid, dropcounter.amount, droptable.dropname
FROM droptable
JOIN dropcounter
ON droptable.boss_id = dropcounter.boss_id
WHERE dropcounter.drop_id = droptable.drop_id AND droptable.boss_id = 1 AND dropcounter.userid = 1
ORDER BY dropcounter.amount DESC;
它可以正常工作和排序。