繁体   English   中英

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

[英]Selecting From Database Using 2 variables

我正在尝试实现的是使用另一个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`)

发送者或接收者=用户ID

where ? in (sender,receiver)

暂无
暂无

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

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