如何从数据库表中选择除某些id之外的内容

发布于 2024-12-12 01:30:09 字数 343 浏览 0 评论 0原文

我的数据库表具有以下 pageid 1,2,3,4,5,6,7,8 -->会添加更多。 我正在使用

$sql ="select * from pages order by pageid";
$result= mysql_query($sql);
while($page= mysql_fetch_array($result)){
echo "$page[pagetitle]";
}

现在我想选择除 pageid=4 之外的所有内容!这样它就不会显示 pageid = 4 的页面标题并显示其他 1,2,3,5,6 --->以及任何其他添加的内容。

怎么说呢,选择所有,除了!

I've database table with the following pageid's 1,2,3,4,5,6,7,8 --> will added more.
and i'm using

$sql ="select * from pages order by pageid";
$result= mysql_query($sql);
while($page= mysql_fetch_array($result)){
echo "$page[pagetitle]";
}

Now i want to select all except pageid=4 ! so that it won't show the page title that have pageid = 4 and show the others 1,2,3,5,6 ---> and any other added.

How to say select all except !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-12-19 01:30:09

只需添加一个 WHERE 子句 WHERE pageid <> 4

$sql ="select * from pages WHERE pageid <> 4 order by pageid";
$result= mysql_query($sql);
while($page= mysql_fetch_array($result)){
  echo $page['pagetitle'];
}

或者替换为 PHP 变量以使其动态:

$exclpage = 4;
$sql ="SELECT * FROM pages WHERE pageid <> $exclpage ORDER BY pageid";

如果要排除多个 pageid,请使用 NOT IN() 子句:

// Exclude pages 4,5,9
$sql ="SELECT * FROM pages WHERE pageid NOT IN (4,5,9) ORDER BY pageid";

更新:

因为这个答案最近有一些注意,建议使用准备好的语句并对变量进行参数化,而不是直接在查询中参数化变量,特别是当变量是任何用户输入的结果时。

$sql ="SELECT * FROM pages WHERE pageid <> ? ORDER BY pageid";
// Bind ? according to your preferred API's method, such as MySQLi::bind_param() or PDO::bindParam()

Simply add a WHERE clause WHERE pageid <> 4:

$sql ="select * from pages WHERE pageid <> 4 order by pageid";
$result= mysql_query($sql);
while($page= mysql_fetch_array($result)){
  echo $page['pagetitle'];
}

Or replace with a PHP variable to make it dynamic:

$exclpage = 4;
$sql ="SELECT * FROM pages WHERE pageid <> $exclpage ORDER BY pageid";

If you have multiple pageids to exclude, use a NOT IN() clause:

// Exclude pages 4,5,9
$sql ="SELECT * FROM pages WHERE pageid NOT IN (4,5,9) ORDER BY pageid";

Update:

Since this answer has more recently had some attention, it is advisable to use prepared statements and parameterize the variables instead of variables directly in the query, particularly if the variables are the result of any user input.

$sql ="SELECT * FROM pages WHERE pageid <> ? ORDER BY pageid";
// Bind ? according to your preferred API's method, such as MySQLi::bind_param() or PDO::bindParam()
套路撩心 2024-12-19 01:30:09

就在今天,我做了类似的事情,但是使用了子查询(我使用 t-sql):

select * from pages 
WHERE pageid NOT IN (select pageid from myTable where 'some conditions...') 
order by pageid

希望你发现它有用

Just today I did something like this, but with a subquery (I use t-sql):

select * from pages 
WHERE pageid NOT IN (select pageid from myTable where 'some conditions...') 
order by pageid

Hope you find it usefull

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文