简体   繁体   English

如何将多个选择查询合并为一个?

[英]how to combine multiple select query into one?

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 在这里,我要从Reference表中提取与Product_property和Categories_id有关的Product_reference值,作为

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 . 现在,此Product_reference是product_details表中的字段名称,假设结果为abc So i want to extract distinct abc from product_details table as 所以我想从product_details表中提取不同的abc作为

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. 正如Eggyal所指出的,由于category_id = 1和category_id = 3的不可能条件,这将始终不返回任何内容,因此:它们都必须为1或都必须为3,或者它们是完全不同的是无关的,或者恰好具有相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM