【问题标题】:Complicated sql statement with 4 conditions inside复杂的sql语句,里面有4个条件
【发布时间】:2013-08-31 16:17:59
【问题描述】:

我正在尝试从下表中查询 sql。我已经尝试了很多方法来完成这项工作。但对于我来说,找到解决方案似乎太复杂了。

user_id="200"; // 假设用户 ID 现在是 200。

tb_conversation

-------------------------------------------------------------------------------------
c_id   |   user_one_id   |   user_two_id   |   user_one_delmsg    |   user_two_delmsg
-------------------------------------------------------------------------------------
001    |      200        |      198        |          Y           |        N
------------------------------------------------------------------------------------
002    |      195        |      200        |          Y           |        N
------------------------------------------------------------------------------------
003    |      200        |      193        |          N           |        N
------------------------------------------------------------------------------------

我要做的是查询与上面的 user_id 匹配的唯一一个表。 它可以是表中的 user_one 或 user_two。如果 user_id 在表中是 user_one,那么 user_one_delmsg 不能是“Y”。或者如果表中的user_id是user_two,那么user_two_delmsg不能是“Y”

我尝试过的:

$q= "SELECT * from conversation  ORDER BY c_id DESC ";
$_stmt = $conn->prepare($q);
$_stmt->execute();
$row=$_stmt->fetchAll();


foreach ($row as $r) {

if ($user_id==$r['user_one_id']){
    if ( $r['user_one_delmsg']!="Y") {
    //do something

    }
}

if ($user_id==$r['user_two_id']){

    if ( $r['user_two_delmsg']!="Y") {

        //do something

    }
    }

我得到的是: 与查询匹配的结果数组。 但是我想要的只是一个结果,即最大 c_id 和 user_x_delmsg 不得为“Y”

我也只使用了 fetch();我没有得到我想要的。 我也将限制 1 放在查询的最后,但没有帮助。

【问题讨论】:

  • 你想要的结果是什么?是 3 吗? c_id 的列类型是什么?

标签: mysql sql


【解决方案1】:

对于给定的用户 ID,尝试

 Select Max(c_id) from conversation 
 Where 200 in (user_one_id, user_two_id)
     And (user_one_id <> 200 Or user_one_delmsg <> 'Y')
     And (user_two_id <> 200 Or user_two_delmsg <> 'Y')

对于所有 UserId,请尝试:

Select userId , Max(cid) From
 (Select c_id cid, user_one_id userId 
  from conversation
  Where user_one_delmsg <> 'Y'
  Union
  Select c_id cid, user_two_id userId 
  from conversation
  Where user_one_delmsg <> 'Y') Z
Group By UserId

【讨论】:

  • 我也不工作 :S 你认为是因为 pdo 吗?我已将 200 替换为 $user_id
  • 当我移除 Max 后,它就可以工作了!但它没有给我最高 id :S
  • 您在使用Max() 时遇到了什么错误?这就是给出顶部 id 的原因。
  • 现在我想知道一件事……突然间,你给我的方法在没有 Max 的情况下也能正常工作。为什么?如果是这样,为什么我们需要 Max 呢?请解释一下。
  • 没有最大值,只有只有一个满足条件的对话才会起作用。如果有多个,则全部返回,而不仅仅是c_id最高的那个
【解决方案2】:

尝试使用以下查询

SELECT MAX(c_id) FROM tb_conversation
   WHERE (user_one_id=200 AND user_one_delmsg='N')
      OR (user_two_id=200 AND user_two_delmsg='N')

检查这个Fiddle

【讨论】:

    【解决方案3】:

    这将选择 max(c_id) 并检查 user_one_delmsg 是否不等于 y。

    select max(c_id), user_one_id from conversation where user_one_delmsg!='y';
    

    这将为 user_one_id 和 user_two_id(特别是提到的 200)选择 max(c_id) 并将检查 user_one_delmsg。

    select max(c_id), user_one_id from conservation where user_one_id='200' and 
    user_one_delmsg!='y' union select max(c_id), user_two_id from conservation where 
    user_two_id='200' and user_two_delmsg!='y';
    

    【讨论】:

    • @PoramatFin 好的,但是请编辑您的问题并提及所需的输出,这将有助于其他人回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2015-06-07
    相关资源
    最近更新 更多