简体   繁体   English

OPERATOR 中的多个 SQL 查询

[英]Multiple SQL query IN OPERATOR

I tried to statements我试图陈述

SELECT s.*
FROM status s
WHERE s.reg IN (Select reg from sdel) OR
      s.reg IN (Select reg from ddel);

The above mentioned statement works out but when I tried the below mentioned statement, it didn't extract any results for me.上面提到的语句有效,但是当我尝试下面提到的语句时,它没有为我提取任何结果。

 SELECT s.*
    FROM status s
    WHERE s.reg IN (Select reg,pic from sdel where sdel.reg = status.reg) OR
          s.reg IN (Select reg,pic from ddel where ddel.reg = status.reg) OR
          s.reg IN (Select reg,pic from gdel where gdel.reg = status.reg) OR
          s.reg IN (Select reg,pic from ip where ip.reg = status.reg);

Do this with two in expressions:使用两个in表达式执行此操作:

SELECT s.*
FROM status s
WHERE s.reg IN (Select reg from sdel) OR
      s.reg IN (Select reg from ddel);

You can also use UNION ALL for one query.您还可以将UNION ALL用于一个查询。 But two separate queries give more opportunity for optimization.但是两个单独的查询提供了更多的优化机会。

In your second query you extract both the attributes reg and pic and the compare with the only attribute reg , so the result is always false.在您的第二个查询中,您提取了属性regpic以及与唯一属性reg的比较,因此结果始终为 false。

If you need to compare only reg , then remove pic from the internal select clause, like in:如果您只需要比较reg ,则从内部 select 子句中删除pic ,例如:

$res=mysql_query("SELECT s.*
    FROM status s
    WHERE s.reg IN (Select reg from sdel where sdel.reg = status.reg) OR
          s.reg IN (Select reg from ddel where ddel.reg = status.reg) OR
          s.reg IN (Select reg from gdel where gdel.reg = status.reg) OR
          s.reg IN (Select reg from ip where ip.reg = status.reg);");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM