0

我正在尝试编写 SQL 语句,但遇到了麻烦。我有以下表格

笔记

person_user VARCHAR
message VARCHAR
time DATETIME
id INT (primary Key)

name (primary key)
address VARCHAR
telephone INT

有关联

person_associating VARCHAR
person_associating_with VARCHAR

我需要出现笔记,只有表格中写person_associating_with的笔记。Associated_With

这是我现在的尝试:

SELECT * 
FROM Notes 
INNER JOIN Notes.person_user ON Notes.id AS associatedMessages
INNER JOIN Associated_With On associatedMessages
WHERE 
     Person.name = person_associating
ORDER BY 
     time DESC;

目前,无论查询如何,我都会返回所有可能的结果

4

2 回答 2

1

我假设您正在寻找以下内容:

SELECT *
FROM Notes AS ABB2 
   JOIN Associated_With AS ABB1 ON 
       ABB1.person_associating_with = ABB2.person_user
ORDER BY time DESC

在您的原始查询中,您尝试连接具有 ON 条件和 WHERE 条件的表。虽然可以这样做,但最好使用其中一种以保持一致性。

于 2013-11-03T19:45:12.790 回答
0

如果我理解正确,您可能不是在寻找联接,而是在寻找 IN 语句:

SELECT *
FROM Notes
WHERE person_user IN
    (SELECT person_associating_with
     FROM Associated_With);
于 2013-11-03T19:38:51.750 回答