从数据库中选择使用2个变量

时间:2015-10-25 23:00:42

标签: mysql sql database laravel

我想要实现的是使用另一个select中的select从数据库中获取数据。我成功导入了sender = $id,但是我希望返回两者的记录:

  • sender = $id && receiver = any user

  • receiver = $id && sender = any user

这是我的代码:

$comments = \DB::select('select * from
                           (select * from comments c
                               where `sender` = ?     //(here) 
                               order by created_at desc) am
                           group by conversation_id
                           order by created_at desc
                           LIMIT 10',
                           [\Auth::user()->id]);

使用这些会导致错误:

where ? = (sender, receiver)
  

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sender, receiver)

where ? = (`sender`, `receiver`)

SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1
column(s) (SQL: select * from (select * from comments m
where Test = (`from`, `to`)

1 个答案:

答案 0 :(得分:1)

发件人或收件人=用户ID

where ? in (sender,receiver)