Solved

MySQL many to many query

Posted on 2011-02-12
1
365 Views
Last Modified: 2012-05-11
Hi I have the following tables:

-- phpMyAdmin SQL Dump
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Feb 09, 2011 at 08:20 PM
-- Server version: 5.1.36
-- PHP Version: 5.3.0

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- Table structure for table `c`
--

CREATE TABLE IF NOT EXISTS `c` (
  `category_id` int(11) NOT NULL,
  `category_name` varchar(100) COLLATE latin1_german2_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci;

--
-- Dumping data for table `c`
--

INSERT INTO `c` (`category_id`, `category_name`) VALUES
(1, 'cat-1'),
(2, 'cat-2'),
(3, 'cat-3'),
(4, 'cat-4');

-- --------------------------------------------------------

--
-- Table structure for table `projects`
--

CREATE TABLE IF NOT EXISTS `projects` (
  `project_id` int(11) NOT NULL AUTO_INCREMENT,
  `project_name` varchar(100) COLLATE latin1_german2_ci NOT NULL,
  `thumb` varchar(100) COLLATE latin1_german2_ci NOT NULL,
  `project_visibility` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`project_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=4 ;

--
-- Dumping data for table `projects`
--

INSERT INTO `projects` (`project_id`, `project_name`, `thumb`, `project_visibility`) VALUES
(1, 'a', 'a-thumb', 1),
(2, 'b', 'b-thumb', 0),
(3, 'c', 'c-thumb', 1);

-- --------------------------------------------------------

--
-- Table structure for table `p_c`
--

CREATE TABLE IF NOT EXISTS `p_c` (
  `project_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci;

--
-- Dumping data for table `p_c`
--

INSERT INTO `p_c` (`project_id`, `category_id`) VALUES
(1, 3),
(1, 4),
(2, 1),
(2, 4),
(3, 2);

Open in new window



I currently have this query:
SELECT * FROM projects WHERE project_name = %s

Open in new window

which displays a project row based on the project_name passed via url variable.

I however would also like to return the category_names which this project belongs to and other project_names that have project_visibility set to "1" and  match this projects category_names


0
Comment
Question by:sany101
1 Comment
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 34880677
SELECT c.category_name, 
       p.project_name 
  FROM projects AS p 
       JOIN p_c AS pc 
         ON p.project_id = pc.project_id 
       JOIN c 
         ON pc.category_id = c.category_id 
 WHERE p.project_name = %s 
       AND p.project_visibility = 1

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now