MySQL:AND运算符不起作用

时间:2017-05-22 10:28:46

标签: php mysql phpmyadmin

我聊天数据库包含三个表(users,message,message_recipient) 和他们在这里的关系: Database Design 表的内容是:

tabel用户:

us_id username password 1 user1 pass1 2 user2 pass2 3 user3 pass3

表mesasage

id  msg_body   create_date  creator_id
1   "Hi user2" ---------      1
2   "Hi user3  ---------      1

表message_recipient

id  repient_id  message_id is_read
1   2           1          0
2   3           2          0

我的问题是当选择user1发送给user2的消息时,AND操作符无法正常工作或者存在AND错误 SQL查询:

SELECT message.id, message.msg_body,message.creator_id,message_recipient.recipient_id
FROM message,message_recipient
WHERE message.creator_id='1' AND message_recipient.recipient_id='2'

输出是:

id msg_body    creator_id    recipient_id
1  "Hi user2"  1             2
2  "Hi user3"  1             2

3 个答案:

答案 0 :(得分:1)

您需要使用JOIN来执行此操作: -

SELECT message.id, message.msg_body,message.creator_id,message_recipient.recipient_id
FROM message JOIN message_recipient ON message.id = message_recipient.message_id
WHERE message.creator_id='1' AND message_recipient.recipient_id='2'

注意: - 当您尝试从两个或更多表中获取数据时,您需要使用不同类型的Joins

参考: - https://www.w3schools.com/sql/sql_join.asp

答案 1 :(得分:0)

您需要在消息ID上加入它们。试试这个:

(define (cars lists-of-pair)
  (call/cc (lambda (exit)
    (fold (lambda (e a)
            (if (pair? e)
                (cons (car e) a)
                (exit '()))) ; throw away continuations to make current result make it ()
          '()
          lists-of-pair)))

(cars '((1 2) (a b))) ; ==> (1 a)
(cars '((1 2) ()))    ; ==> ()

答案 2 :(得分:0)

我在代码中的where message.id = message_recipient.message_id中添加了这个,并且它可以正常工作

我们可以使用where子句连接多个表。

SELECT message.id, 
message.msg_body,message.creator_id,message_recipient.recipi‌​ent_id FROM 
message,message_recipient WHERE message.id = message_recipient.message_id and 
message.creator_id='1' AND message_recipient.recipient_id='2'