我正在使用联接从两个表中获取消息
表格消息: ID,发件人,收件人,消息,密码
表格自有留言:ID,发件人,收件人,留言,密码
查询:
SELECT DISTINCT(Messages.ID) as F_id,
Messages.Message as F_message,
Messages.Password as F_password,
Messages.Receiver as F_receiver,
OwnMessages.ID as O_id,
OwnMessages.Message as O_message,
OwnMessages.Password as O_password,
OwnMessages.Sender as O_sender
FROM Messages
JOIN OwnMessages on Messages.Receiver = OwnMessages.Sender
WHERE Messages.Receiver = 8 AND OwnMessages.Sender = 8
表中的所有内容消息显示正确但是从表OwnMessages我得到第一行30次。有什么想法吗?
答案 0 :(得分:0)
SELECT DISTINCT
Messages.ID as F_id,
Messages.Message as F_message,
Messages.Password as F_password,
Messages.Receiver as F_receiver,
OwnMessages.ID as O_id,
OwnMessages.Message as O_message,
OwnMessages.Password as O_password,
OwnMessages.Sender as O_sender
FROM Messages
INNER JOIN OwnMessages on Messages.Receiver = OwnMessages.Sender
WHERE Messages.Receiver = 8 AND OwnMessages.Sender = 8
或
SELECT Messages.ID as F_id,
Messages.Message as F_message,
Messages.Password as F_password,
Messages.Receiver as F_receiver,
OwnMessages.ID as O_id,
OwnMessages.Message as O_message,
OwnMessages.Password as O_password,
OwnMessages.Sender as O_sender
FROM Messages
INNER JOIN OwnMessages on Messages.Receiver = OwnMessages.Sender
WHERE Messages.Receiver = 8 AND OwnMessages.Sender = 8
GROUP BY Messages.ID
HAVING COUNT(*) = 1