多个AND条件和OR条件

时间:2018-04-10 08:16:50

标签: mysql

我正在尝试使用以下查询从两个表中检索数据。

SELECT DISTINCT a.id,a.company_name,a.company_logo 
FROM   eco_company_profile a, eco_product_desc ep 
where (a.status='t' AND ep.status='t' AND a.`pro_thunderx` ='yes') 
OR    (a.status='t' AND ep.status='t' AND ep.`pro_thunderx` ='yes') 
order by a.company_name

但它从表中返回了所有公司的详细信息。 我需要在以下条件下检索所有公司详细信息,

eco_company_profile.status =t and
eco_company_profile.pro_thunderx =yes and
eco_product_desc.status =t

或者

eco_company_profile.status =t and
eco_product_desc.pro_thunderx =yes and
eco_product_desc.status =t

3 个答案:

答案 0 :(得分:0)

...试

SELECT DISTINCT a.id,a.company_name,a.company_logo 
FROM   eco_company_profile a, eco_product_desc ep 
where a.status='t' AND ep.status='t' AND a.`pro_thunderx` ='yes' OR ep.`pro_thunderx` ='yes'
order by a.company_name

答案 1 :(得分:0)

你可以试试这个。

SELECT DISTINCT a.id,a.company_name,a.company_logo 
FROM   eco_company_profile a, eco_product_desc ep 
where a.status='t' AND ep.status='t' AND (a.pro_thunderx ='yes' OR ep.pro_thunderx ='yes') 
order by a.company_name

答案 2 :(得分:-1)

最后我得到了解决方案。忘了为两个表添加唯一ID   SELECT DISTINCT a.id,a.company_name,a.company_logo FROM eco_company_profile a, eco_product_desc ep WHERE (ep.status='t' AND a.status='t' AND ep.pro_thunderx ='yes' and a.id=ep.cid) OR (a.status='t' AND a.pro_thunderx ='yes') ORDER BY a.company_name