单个查询而不是多个select语句

时间:2015-09-18 21:01:18

标签: mysql sql select

你好,我陷入了SQL语句的困境。任何人都可以帮助我声明:

表1

@PayloadRoot(localPart = "doRequest", namespace = "http://www.varun.com/RetrievalService")
public String retrieveCustomerProfileFromDoReq( String string ) {
     // business processing
    return "Success ::"+ string ;
}

表2。

Customers
customer_id | customer_name | custom_url | customer_desc

Categories category_id | customer_id | category_name

给我一​​份客户ID列表。现在我用它们从customers表中找到他们所有的名字,desc和url。

我需要在单个查询中执行此操作,并处理客户详细信息以显示在UX上。我真的不知道如何做到这一点。

这就是我所拥有的(我知道错了)

select customer_id where category_name="Realtor";

2 个答案:

答案 0 :(得分:2)

将您的查询修改为JOIN查询,例如

select c.* 
from customers c
join categories ca on c.customer_id = ca.customer_id
where ca.category_name='Realtor';

答案 1 :(得分:0)

您可以使用INANYEXISTS执行您想要的操作:

select *
from customers
where customer_id = ANY (select customer_id
                         from categories
                         where category_name = 'Realtor'
                        );

或者:

select *
from customers
where customer_id IN (select customer_id
                      from categories
                      where category_name = 'Realtor'
                     );