views:

56

answers:

2

Hi, I have 3 tables: projects, skills and project_skills. In projects table i hold project's general data. Second table skills i hold skill id and skill name also i have projects_skills table which is hold project's skill relationships. Here is scheme of tables:

CREATE TABLE IF NOT EXISTS `project_skills` (
  `project_id` int(11) NOT NULL,
  `skill_id` int(11) NOT NULL,
  KEY `project_id` (`project_id`),
  KEY `skill_id` (`skill_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci;


CREATE TABLE IF NOT EXISTS `projects` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `employer_id` int(11) NOT NULL,
  `project_title` varchar(100) COLLATE utf8_turkish_ci NOT NULL,
  `project_description` text COLLATE utf8_turkish_ci NOT NULL,
  `project_budget` int(11) NOT NULL,
  `project_allowedtime` int(11) NOT NULL,
  `project_deadline` datetime NOT NULL,
  `total_bids` int(11) NOT NULL,
  `average_bid` int(11) NOT NULL,
  `created` datetime NOT NULL,
  `active` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `created` (`created`),
  KEY `employer_id` (`employer_id`),
  KEY `active` (`active`),
  FULLTEXT KEY `project_title` (`project_title`,`project_description`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `skills` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category` int(11) NOT NULL,
  `name` varchar(100) COLLATE utf8_turkish_ci NOT NULL,
  `seo_name` varchar(100) COLLATE utf8_turkish_ci NOT NULL,
  `total_projects` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `seo_name` (`seo_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=224 ;

I want to select projects with related skill names. I think i have to use JOIN but i don't know how can i do. Thanks

+1  A: 
select * from projects
left join project_skills on projects.id = project_skills.project_id
left join skills on project_skills.skills_id = skills .id

note: you don't need all columns, but this will let you see what is going on before you pick the columns you need.

Hogan
That will only return projects with skills associated, not related skill names
OMG Ponies
@OMG : I believe it will return skills.name please explain
Hogan
+1  A: 

It sounds like a JOIN is exactly right:

SELECT ...
FROM projects
INNER JOIN project_skills ON (project_skills.project_id = projects.id)
INNER JOIN skills ON (skills.id = project_skills.skill_id)
VoteyDisciple
That will only return projects with skills associated, not related skill names
OMG Ponies
Names of skills are stored in skills.name column; if the names are desired that column can simply be added to the list of fields `SELECT`ed
VoteyDisciple