【问题标题】:Selecting From Database Using 2 variables使用 2 个变量从数据库中选择
【发布时间】:2016-01-24 23:32:40
【问题描述】:

我想要实现的是使用另一个选择中的选择从数据库中获取数据。我成功导入了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`)

【问题讨论】:

    标签: mysql sql database laravel


    【解决方案1】:

    发送者或接收者 = 用户 ID

    where ? in (sender,receiver)
    

    【讨论】:

    • 无需将in改为=
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2010-10-30
    • 2015-05-12
    相关资源
    最近更新 更多