在Mysql查询中使用GROUP_CONCAT

时间:2013-02-28 10:51:47

标签: mysql sql

我正在使用我的数据库中的五个表。一切顺利,直到我开始使用group concat从数据库中选择状态。

这些是我的表格:

  • main_jobs
  • sub_jobs
  • 类别
  • state_job_relationship

我创建表格的片段

create table if not exists category (
    id int(3) not null auto_increment,
    parent int(5) not null,
    name varchar(255) not null,
    description text,
    slug varchar(255),
    level int (3),
    PRIMARY KEY (id, slug));

create table if not exists state (
        id int(4) not null auto_increment,
        country_id int(11) not null,
        name varchar(255) not null,
        PRIMARY KEY (id),
        FOREIGN KEY (country_id) REFERENCES country (id))";

create table if not exists state_job_relationship (
        id int(4) not null auto_increment,
        state_id int(4) not null, FOREIGN KEY (state_id) REFERENCES state (id),
        job_id int(11) not null, FOREIGN KEY (job_id) REFERENCES sub_jobs (id),
        PRIMARY KEY (id));

create table if not exists main_jobs (
            id int not null auto_increment, primary key(id),
            company_name varchar(255),
            job_title varchar(255));

create table if not exists sub_jobs (
            id int not null auto_increment, primary key(id),
            parent_id int(11) not null, FOREIGN KEY (parent_id) REFERENCES main_jobs (id),
            title varchar(255),
            description text not null,
            category int (3), FOREIGN KEY (category) REFERENCES category (id)
            );

这是我想要选择的:

子作业表中的作业标题,以及相应的公司名称以及main_jobs和类别表中的其他详细信息。

一切顺利,直到我需要选择每个sub_job以这种格式出现的状态:State1,State2,State3

select sub_jobs.id as kid, main_jobs.id as parentid, 
  company_name, sub_jobs.description, sub_jobs.title, 
  job_title, category.name as ind, 
  DATE_FORMAT($column_date,'%a, %e %b %Y %T') as d, 
  (select group_concat(name seperator ', ') 
   from state_job_relationship, state 
   where job_id= kid 
     and state.id=state_job_relationship.state_id 
   group by state.id) states 
from sub_jobs, category, main_jobs 
where category.id=sub_jobs.category 
  and main_jobs.id=sub_jobs.parent_id 
order by featured desc , $table.id desc 
limit 100;

尝试上面的查询但显示此错误:

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在state_job_relationship附近的'seperator',')附近使用正确的语法,其中job_id = kid和state.i'在第1行

请问我做对了什么?

1 个答案:

答案 0 :(得分:0)

看起来您的错误是因为您在相关子查询中使用了alais kid。您应该将kid替换为sub_jobs.id

此外,seperator拼写错误。

所以你的代码将是:

select s.id as kid, 
  m.id as parentid, 
  company_name, 
  s.description, 
  s.title, 
  job_title, 
  c.name as ind, 
  DATE_FORMAT($column_date,'%a, %e %b %Y %T') as d, 
  (select group_concat(name SEPARATOR ', ') 
   from state_job_relationship
   inner join state 
     on state.id=state_job_relationship.state_id 
   where job_id= s.id 
   group by state.id) states 
from sub_jobs s
inner join category c
  on c.id=s.category 
inner join main_jobs m
  on m.id=s.parent_id 
order by featured desc , $table.id desc 
limit 100;

您会注意到我更改了语法以使用ANSI JOIN语法,并且我正在使用表的别名。

我的另一个建议是使用子查询并加入子查询:

select s.id as kid, 
  m.id as parentid, 
  company_name, 
  s.description, 
  s.title, 
  job_title, 
  c.name as ind, 
  DATE_FORMAT($column_date,'%a, %e %b %Y %T') as d, 
  st.states 
from sub_jobs s
inner join category c
  on c.id=s.category 
inner join main_jobs m
  on m.id=s.parent_id 
left join
(
  select job_id, state.id, group_concat(name SEPARATOR ', ') states
  from state_job_relationship
  inner join state 
    on state.id=state_job_relationship.state_id 
  group by state.id, job_id
) st
  on st.job_id= s.id
order by featured desc , $table.id desc 
limit 100;