MySQL查询从数据库中选择一个值等于两个不同事物之一的记录

时间:2022-11-29 15:43:19

I have to get records from my MySQL DB where:

我必须从MySQL DB获取记录,其中:

sentto = "$username"

OR

sentto = "everyone"

How would I put this in a MySQL query? I tried a few things, but they don't seem to be working:

我如何将它放在MySQL查询中?我尝试了一些东西,但它们似乎没有起作用:

mysql_query("SELECT * 
             FROM pmessages 
             WHERE status='unread' 
                 AND sentto='$username' || sentto='everyone'");

mysql_query("SELECT * 
             FROM pmessages 
             WHERE status='unread' 
             AND sentto='$username' 
                 AND sentto='everyone'");

I seem to be stumped, if anyone knows how to do this the right way please let me know. This is a new scenario for me. Thank you!

我似乎很难过,如果有人知道如何以正确的方式做到这一点请告诉我。这对我来说是一个新的场景。谢谢!

5 个解决方案

#1


SELECT * 
FROM pmmessages 
WHERE sentto = '$username' 
    OR sentto = 'everyone'

Edit Chris, based on your new query of:

根据您的新查询编辑Chris:

SELECT * 
FROM pmessages 
WHERE status='unread' 
    AND sentto='$username' 
OR sentto='everyone' 

You need to modify it so that your AND stands alone (it is conflicting with your OR).

你需要修改它,以便你的AND独立(它与你的OR相冲突)。

Rewrite it to this

把它改写成这个

SELECT * 
FROM pmessages 
WHERE status='unread' 
    AND 
        (sentto='$username' 
            OR sentto='everyone' )

#2


Taking the detail from one of your comments into account - use the " OR " keyword and parentheses to make sure that the right conditions are combined.

从您的一条评论中考虑细节 - 使用“OR”关键字和括号确保合并正确的条件。

SELECT * FROM pmessages WHERE
   status = 'unread'
    AND
  (sentto = ? OR sentto = 'everyone')

Your problem was never with the OR, though, it was actually the AND precedence and lack of parentheses. The very significant detail that you completely omitted from your question was the additional test for "status = unread".

你的问题从未与OR有关,但它实际上是AND优先级和缺少括号。您在问题中完全忽略的非常重要的细节是“status = unread”的附加测试。

Note the use of ? above - you should really, really use prepared statements whenever combining MySQL and PHP, i.e.:

注意使用?上面 - 你应该在组合MySQL和PHP的时候真正地使用预处理语句,即:

$sql = "..." # as above
$sth = $db->prepare($sql);
$res = $sth->execute($username);
while ($row = $sth->fetchrow()) {
    ...
}

(or the mysqli equivalent)

(或mysqli等价物)

#3


As it's the same column you're testing, I would use the IN keyword:

由于它是您正在测试的相同列,我将使用IN关键字:

SELECT * 
FROM pmessages 
WHERE status='unread' 
AND sentto IN ('everyone', '$username');

#4


The word OR is what you're looking for:

OR是你正在寻找的:

mysql_query("SELECT * FROM pmessages WHERE sentto='$username' OR sentto='everyone'");

mysql_query(“SELECT * FROM pmessages WHERE sentto ='$ username'或sentto ='everyone'”);

#5


is everyone an actual value or do you want to return all results?

每个人都是实际价值还是你想要归还所有结果?

if you want to return all results set up something like this

如果你想返回所有结果设置这样的东西

if (@sentto is null)
begin

     set @sendto='%'

end

mysql_query("SELECT * FROM pmessages WHERE sentto='$username'")

#1


SELECT * 
FROM pmmessages 
WHERE sentto = '$username' 
    OR sentto = 'everyone'

Edit Chris, based on your new query of:

根据您的新查询编辑Chris:

SELECT * 
FROM pmessages 
WHERE status='unread' 
    AND sentto='$username' 
OR sentto='everyone' 

You need to modify it so that your AND stands alone (it is conflicting with your OR).

你需要修改它,以便你的AND独立(它与你的OR相冲突)。

Rewrite it to this

把它改写成这个

SELECT * 
FROM pmessages 
WHERE status='unread' 
    AND 
        (sentto='$username' 
            OR sentto='everyone' )

#2


Taking the detail from one of your comments into account - use the " OR " keyword and parentheses to make sure that the right conditions are combined.

从您的一条评论中考虑细节 - 使用“OR”关键字和括号确保合并正确的条件。

SELECT * FROM pmessages WHERE
   status = 'unread'
    AND
  (sentto = ? OR sentto = 'everyone')

Your problem was never with the OR, though, it was actually the AND precedence and lack of parentheses. The very significant detail that you completely omitted from your question was the additional test for "status = unread".

你的问题从未与OR有关,但它实际上是AND优先级和缺少括号。您在问题中完全忽略的非常重要的细节是“status = unread”的附加测试。

Note the use of ? above - you should really, really use prepared statements whenever combining MySQL and PHP, i.e.:

注意使用?上面 - 你应该在组合MySQL和PHP的时候真正地使用预处理语句,即:

$sql = "..." # as above
$sth = $db->prepare($sql);
$res = $sth->execute($username);
while ($row = $sth->fetchrow()) {
    ...
}

(or the mysqli equivalent)

(或mysqli等价物)

#3


As it's the same column you're testing, I would use the IN keyword:

由于它是您正在测试的相同列,我将使用IN关键字:

SELECT * 
FROM pmessages 
WHERE status='unread' 
AND sentto IN ('everyone', '$username');

#4


The word OR is what you're looking for:

OR是你正在寻找的:

mysql_query("SELECT * FROM pmessages WHERE sentto='$username' OR sentto='everyone'");

mysql_query(“SELECT * FROM pmessages WHERE sentto ='$ username'或sentto ='everyone'”);

#5


is everyone an actual value or do you want to return all results?

每个人都是实际价值还是你想要归还所有结果?

if you want to return all results set up something like this

如果你想返回所有结果设置这样的东西

if (@sentto is null)
begin

     set @sendto='%'

end

mysql_query("SELECT * FROM pmessages WHERE sentto='$username'")