0

i have three tables with structure

CREATE TABLE IF NOT EXISTS `refrence` (
  `products_ref_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `categories_id` int(11) NOT NULL,
  `Product_property` varchar(255) NOT NULL,
  `Product_reference` varchar(50) NOT NULL,
  PRIMARY KEY (`products_ref_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;


CREATE TABLE IF NOT EXISTS `product_details` (
  `products_id` int(11) NOT NULL AUTO_INCREMENT,
  `categories_id` int(11) NOT NULL,
  `FieldA` varchar(30) NOT NULL,
  `FieldB` varchar(50) NOT NULL,
  `FieldC` varchar(255) NOT NULL,
  `FieldD` varchar(255) NOT NULL,
  `FieldE` varchar(255) NOT NULL,
  `FieldF` varchar(255) NOT NULL,
  `Field_desc` text NOT NULL,
  PRIMARY KEY (`products_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=98 ;

Here i am extracting Product_reference value from refrence table with respect to Product_property & categories_id as

SELECT Product_reference FROM refrence where categories_id=3 AND Product_property ='xxx'

Now this Product_reference is the field name in product_details table suppose result is abc. So i want to extract distinct abc from product_details table as

SELECT Distinct abc FROM product_details where categories_id=3

Now i want to combine both the query to one, What is the way to write these queries in to single one.

I tried this

SELECT Distinct (
SELECT Product_reference FROM refrence where categories_id=3 AND Product_property ='xxx'
) as aa FROM product_details where categories_id=3

But its not working, may be i am missing some syntax.

4
  • 1
    "Bit its not working" - Can you be more specific? What's the error? Commented Apr 28, 2012 at 13:36
  • categories_id is it same for all table....I mean are you using it as foreign key? Commented Apr 28, 2012 at 13:43
  • From looking at your query, it's not very clear why you're searching refrence for records where categories_id=3 yet product_details for records where categories_id=1 - and your explanation of what your query is intended to do is very hard to understand. Perhaps you can clarify? Commented Apr 28, 2012 at 13:44
  • @RohitkumarChoudhary yes categories_id is foreign key. Commented Apr 28, 2012 at 17:26

1 Answer 1

1

Without knowing what isn't working, it would appear that you're trying to accomplish this:

SELECT r.product_reference
FROM refrence AS r 
LEFT JOIN categories AS c ON (c.categories_id = r.categories_id AND r.categories_id=3 AND     r.product_property='zzz')
WHERE c.categories_id=1

As pointed out by Eggyal, this will always return nothing because of the impossible condition of category_id=1 AND category_id=3, so either: They both have to be 1 or both have to be 3 OR They're completely different "categorys" that are unrelated, or just happen to have the same names.

Sign up to request clarification or add additional context in comments.

2 Comments

That is ALWAYS going to return an empty recordset due to conflicting filters on categories_id, on which the tables have been joined.
Oops...darned good point..cut/paste too quickly from my editor without trying enough data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.