MySQL创建视图问题,

发布于 2024-08-01 13:09:55 字数 535 浏览 1 评论 0原文

我使用一个视图作为表来连接 3 个表上的数据:

create view category_list as 
select forum_categories.*, max( forum_answer.a_id ) as latest_answer_id 
from forum_categories 
left join forum_question on forum_question.catid = forum_categories.id 
left join forum_answer on forum_answer.question_id = forum_question.id
and forum_answer.qtitle = forum_question.topic;

但是,我遇到了两个问题:

  • 仅从 forum_categories 中提取一条记录进行链接
  • 我无法将 forum_answer.qtitle 与 forum_question.topic 相关联

,请告知:)

谢谢。

I'm using a view as a table to join data on 3 tables:

create view category_list as 
select forum_categories.*, max( forum_answer.a_id ) as latest_answer_id 
from forum_categories 
left join forum_question on forum_question.catid = forum_categories.id 
left join forum_answer on forum_answer.question_id = forum_question.id
and forum_answer.qtitle = forum_question.topic;

However, I am experiencing two problems:

  • Only one record from forum_categories is pulled out to be linked with
  • I am unable to associate forum_answer.qtitle with forum_question.topic

please advise :)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-08-08 13:09:55

缺少 group by 子句。 这是我整理的一个非常简单的示例,下面是我用来创建简单测试数据的 sql。

select 
    cat.*
    , max( ans.id )
from cat
left join ques on ques.cat_id = cat.id
left join ans on ans.ques_id = ques.id
group by cat.id, cat.name

create table cat ( id serial, name varchar (10) );
create table ques ( id serial, name varchar (10), cat_id integer );
create table ans ( id serial, name varchar (10), ques_id integer );

insert into cat (name) values ( 'cat 1' );
insert into cat (name)  values ( 'cat 2' );
insert into cat  (name) values ( 'cat 3' );

insert into ques (name, cat_id) values ( 'ques 1', 1 );
insert into ques (name, cat_id)  values ( 'ques 2', 1 );
insert into ques (name, cat_id)  values ( 'ques 3', 1 );
insert into ques (name, cat_id)  values ( 'ques 4', 2 );
insert into ques (name, cat_id)  values ( 'ques 5', 2 );
insert into ques (name, cat_id)  values ( 'ques 6', 2 );
insert into ques (name, cat_id)  values ( 'ques 7', 2 );

insert into ans (name, ques_id)  values ( 'ans 1', 1 );
insert into ans (name, ques_id)  values ( 'ans 2', 2 );
insert into ans (name, ques_id)  values ( 'ans 3', 2 );
insert into ans (name, ques_id)  values ( 'ans 4', 1 );
insert into ans (name, ques_id)  values ( 'ans 5', 2 );
insert into ans (name, ques_id)  values ( 'ans 6', 1 );
insert into ans (name, ques_id)  values ( 'ans 7', 1 );
insert into ans (name, ques_id)  values ( 'ans 8', 1 );
insert into ans (name, ques_id)  values ( 'ans 9', 2 );
insert into ans (name, ques_id)  values ( 'ans 10', 2 );

The group by clause is missing. Here's a very simple example I put together, and below it the sql I used to create the simple test data.

select 
    cat.*
    , max( ans.id )
from cat
left join ques on ques.cat_id = cat.id
left join ans on ans.ques_id = ques.id
group by cat.id, cat.name

create table cat ( id serial, name varchar (10) );
create table ques ( id serial, name varchar (10), cat_id integer );
create table ans ( id serial, name varchar (10), ques_id integer );

insert into cat (name) values ( 'cat 1' );
insert into cat (name)  values ( 'cat 2' );
insert into cat  (name) values ( 'cat 3' );

insert into ques (name, cat_id) values ( 'ques 1', 1 );
insert into ques (name, cat_id)  values ( 'ques 2', 1 );
insert into ques (name, cat_id)  values ( 'ques 3', 1 );
insert into ques (name, cat_id)  values ( 'ques 4', 2 );
insert into ques (name, cat_id)  values ( 'ques 5', 2 );
insert into ques (name, cat_id)  values ( 'ques 6', 2 );
insert into ques (name, cat_id)  values ( 'ques 7', 2 );

insert into ans (name, ques_id)  values ( 'ans 1', 1 );
insert into ans (name, ques_id)  values ( 'ans 2', 2 );
insert into ans (name, ques_id)  values ( 'ans 3', 2 );
insert into ans (name, ques_id)  values ( 'ans 4', 1 );
insert into ans (name, ques_id)  values ( 'ans 5', 2 );
insert into ans (name, ques_id)  values ( 'ans 6', 1 );
insert into ans (name, ques_id)  values ( 'ans 7', 1 );
insert into ans (name, ques_id)  values ( 'ans 8', 1 );
insert into ans (name, ques_id)  values ( 'ans 9', 2 );
insert into ans (name, ques_id)  values ( 'ans 10', 2 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文