如何仅显示与另一个表中的值匹配的行


How to display only rows that match values in another table

在这样的事情中它将如何工作?我什至尝试使用"AND tb1.name !=",但没有奏效。

$area = 0;
$stmt = $dbh->prepare('SELECT * FROM charinfo WHERE current_area != :area ORDER BY current_area');
$stmt->execute(array('area' => $area));
$result = $stmt->fetchAll();
$place = 1;

运行一个游戏服务器,在网站上我有一个前 30 名排行榜,它正在创造奇迹(在这里直接从另一个主题中找到代码),我遇到的问题是无法使用每个人都建议的 JOIN 功能,以防止管理员的角色也被列出。

这是我现在在我的网站上的代码,它显示了排名 1-30、角色名称和表格中的级别。这是它在我的网站上工作

<?php
require("srvcs/config.php");
$stmt = $dbh->prepare('SELECT * FROM chars ORDER BY CAST(experience AS UNSIGNED ) DESC LIMIT 30;');
$stmt->execute();
$result = $stmt->fetchAll();
$place = 1;
echo '<table class="justShowme" style="width:600px;height:150px;">
                        <tr>
                            <td>Rank</td>
                            <td>Character Name</td>
                            <td>Level</td>
                            </tr>';
    foreach ($result as $index => &$item) {
        $exp = floor(pow($item['experience'] + 1, 1/4));
        $name = $item['name'];
        echo '<tr>';
        echo "<td><B>" . $place++ . "</B></td>";
        echo "<td>" . $name . "</td>";
        echo "<td>" . $exp . "</td>";
        echo '</tr>';
    }
    echo '</table></center>';
?>

我对 MySQL 不是很熟悉,所以我首先列出我知道是必要的......

  1. "字符"表包含字符信息

    'sID' column is unique and matches the subscriber 'ID' column, whoever owns the character
    
  2. "订阅者"表包括帐户信息和管理员状态

    'ID' is the subscriber ID which the 'sID' from chars table refers to
    'admin' is the admin status of the account as Y or N
    

如果字符的 sID 值为订阅者 ID,管理员值为 Y,则不应列出该字符。

如果该字符的 sID 值为订阅者 ID 且管理员值为 N,则将列出该字符,并且该表将列为 DESC,并且仅显示 30 行结果。

我将如何做到这一点?

任何帮助将不胜感激!这是我的第一篇文章,所以关于未来帮助请求的提示也会很好:)提前谢谢你!

SELECT tb1.*
    FROM chars tb1
    JOIN subscriber tb2
    ON tb1.sID=tb2.ID
    WHERE admin = 'N'
    ORDER BY CAST(experience AS UNSIGNED ) DESC 
    LIMIT 30;

您可以使用 NOT IN 子查询。

见下文。

SELECT chars.* 
FROM chars 
WHERE sID NOT IN (SELECT ID FROM subscriber WHERE subscriber.admin = 'Y')
ORDER BY CAST(experience AS UNSIGNED ) DESC 
LIMIT 30;