我在我的數據庫中有五個表與我一起工作。一切正常,直到我開始使用組concat從數據庫中選擇狀態。在Mysql查詢中使用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屬於這一格式的狀態:狀態1,狀態2,狀態3
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;
嘗試上述查詢,但它顯示這個錯誤:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'seperator ', ') from state_job_relationship, state where job_id= kid and state.i' at line 1
請問我什麼不對?
請正確格式化你的問題,尤其是你試一下查詢。它也會鼓勵更多的人來幫助你。 – fancyPants 2013-02-28 11:32:05