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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.