How Can i Create This Complicated Query?
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 p开发者_如何学Crojects_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
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)
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.
精彩评论