如何在mysql中过滤相关对象

时间:2018-03-26 13:41:50

标签: mysql sql

有人可以帮我解决这个问题。 我在mysql数据库中有一个这样的用户表:

MariaDB [tiro]> SELECT * FROM user_tb;
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | dias       | maria     |
|  2 | paulo      | heleno    |
|  3 | silas      | raimundo  |
+----+------------+-----------+

这一个问题: 注意:它有一个user_tb的外键。

MariaDB [tiro]> SELECT * FROM question_tb;
+----+-------------------------+-------------+
| id | question                | asked_by_id |
+----+-------------------------+-------------+
|  1 | whats up                |           1 |
|  2 | who is out there        |           1 |
|  3 | where do we party today |           3 |
+----+-------------------------+-------------+

另一个答案: 注意:它有user_tb和question_tb的外键。

MariaDB [tiro]> SELECT * FROM answer_tb;
+----+---------------------------------+-------------+----------------+
| id | answer                          | question_id | answered_by_id |
+----+---------------------------------+-------------+----------------+
|  2 | the man in control of you house |           2 |              1 |
|  3 | nothing at all                  |           1 |              1 |
|  4 | lets party at my house guys     |           3 |              1 |
|  5 | casino is the best place        |           3 |              2 |
|  6 | its your enemy                  |           2 |              2 |
|  7 | we are fuck this world          |           1 |              3 |
+----+---------------------------------+-------------+----------------+

表中包含我需要过滤的所有数据。在这里,我想过滤ID为1的用户有答案但排除其他用户对该问题回答的所有问题的所有问题。

用户ID 1在问题ID 2处具有答案,并且还具有用户ID 2。我怎么能得到问题id 2只有用户1的答案而不是用户2.是否可能?我的大脑被锁在这里,请帮忙。

2 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解了你的意图,但如果我有,我相信答案就像这样简单:

SELECT *
FROM question_tb
JOIN answer_tb ON answer_tb.question_id = question_tb.id
WHERE answered_by_id = 1; <- replace this with your desired ID

此查询只检索所有现有的问题 - 答案对,并筛选出包含来自其他人的答案而非来自指定用户的所有答案。

示例输出:

+----------------+-------------------------+-------------++--------------+---------------------------------+-------------+----------------+
| question_tb.id | question                | asked_by_id || answer_tb.id | answer                          | question_id | answered_by_id |
+----------------+-------------------------+-------------++--------------+---------------------------------+-------------+----------------+
|              1 | whats up                |           1 ||            3 | nothing at all                  |           1 |              1 |
|              2 | who is out there        |           1 ||            2 | the man in control of you house |           2 |              1 |
|              3 | where do we party today |           3 ||            4 | lets party at my house guys     |           3 |              1 |
+----------------+-------------------------+-------------++--------------+---------------------------------+-------------+----------------+

答案 1 :(得分:1)

根据您的问题,您希望得到问题2的答案,但只能得到用户ID为1的用户“dias maria”给出的答案。

一些简单的“WHERE”子句可以实现:

SELECT * 
FROM answer_tb 
WHERE question_id = 2 AND answered_by_id = 1

如果您希望输出额外的详细信息,例如问题文本和用户名,您也可以加入这些表:

SELECT * 
FROM answer_tb 
INNER JOIN user_tb ON user_tb.id = answer_tb.answered_by_id
INNER JOIN question_tb ON question_tb.id = answer_tb.question_id
WHERE answer_tb.question_id = 2 AND answer_tb.answered_by_id = 1

这是非常基本的SQL,所以我建议你做一些研究,如果这让你头痛。