Solved

MySQL many to many query

Posted on 2011-02-12
1
371 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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

17 Experts available now in Live!

Get 1:1 Help Now