我创建了三个表
在这里,我想获取与特定问题(id)相关的所有 question_tags 和答案。对于需要的单个查询或存储过程
create table questions(id varchar(100),title varchar(140),body varchar(2000),
primary key(id));
create table question_tags(id varchar(50),tag_name varchar(50),question_id varchar(100),
primary key(id),foreign key(question_id) references questions(id));
create table answers(id varchar(50),answer varchar(2000),question_id varchar(100),
primary key(id),foreign key(question_id) references questions(id));
以及表中的以下数据
mysql> select * from questions;
+----+-------+-------------+
| id | title | body |
+----+-------+-------------+
| 1 | a | hello |
| 2 | b | hii |
| 3 | c | bye |
| 4 | d | how are you |
+----+-------+-------------+
4 rows in set (0.03 sec)
mysql> select * from question_tags
+----+----------+-------------+
| id | tag_name | question_id |
+----+----------+-------------+
| t1 | java | 1 |
| t2 | mysql | 1 |
| t3 | jquery | 1 |
+----+----------+-------------+
3 rows in set (0.00 sec)
mysql> select * from answers;
+----+-----------+-------------+
| id | answer | question_id |
+----+-----------+-------------+
| a1 | good | 1 |
| a2 | excellent | 1 |
| a3 | ok | 1 |
+----+-----------+-------------+
3 rows in set (0.00 sec)