0

我有两个表[basic][info]都有一个主键email_id。现在我想从basic表中检索信息,但where子句应该从两个表中都有搜索条件

例子:

SELECT DISTINCT 
    basic.f_name, 
    basic.l_name 
FROM 
    basic 
CROSS JOIN 
    info 
WHERE 
    (basic.email_id = basic.email_id) 
    AND (basic.caste LIKE '%' + @caste + '%') 
    AND (info.diet =  @diet )

(此查询是用 asp 为 gridview 编写的,但它不起作用)

4

3 回答 3

6
SELECT basic.f_name, basic.l_name 
FROM basic INNER JOIN info ON basic.email_id = info.email_id 
WHERE (basic.caste LIKE '%' + @caste + '%') AND (info.diet = @diet )
于 2013-03-29T16:13:14.757 回答
1

当您进行内部连接时,您实际上是在使用带有 where 子句的交叉连接。在内部连接中,所有数据都被组合(笛卡尔积),然后使用 where 子句进行过滤。因此,我认为您的代码使用内部连接会更干净。

SELECT DISTINCT basic.f_name, basic.l_name 
FROM basic 
CROSS JOIN info 
WHERE (basic.email_id = basic.email_id) 

相当于

SELECT basic.f_name, basic.l_name 
FROM basic INNER JOIN info ON basic.email_id = info.email_id 

我认为这可以解决您的问题。

于 2013-03-29T16:21:50.897 回答
0

使用内部联接

SELECT DISTINCT 
    basic.f_name, 
    basic.l_name 
FROM 
    basic as basic
INNER JOIN 
    info as info 
    ON basic.email_id = info.email_id
WHERE 
    (basic.caste LIKE '%' + @caste + '%') 
    AND (info.diet =  @diet )
于 2013-03-29T16:24:14.320 回答