如何组合两个SQL查询并有选择地显示结果

时间:2014-04-03 13:45:01

标签: asp.net sql

我有两个查询填充了两个下拉列表。

获取数据库中可用的所有特色

string myQuery1 = "SELECT * FROM content WHERE (folder_id IN (SELECT folder_id FROM [content_folder_tbl] WHERE (parent_id = 158) OR (parent_id = 149))) AND (published = 1) AND (UPPER(content_title) NOT LIKE '%DESCRIPTION') AND (UPPER(content_title) NOT LIKE '%IMAGE') ORDER BY content_title";

获取数据库中可用的所有位置

string myQuery = "SELECT * FROM content WHERE (folder_id = 106) AND (content_type = 1) AND (published = 1) ORDER BY content_title";

如何编写将执行以下内容的查询:

显示所选位置

中可用的专业

当用户按下搜索按钮

1 个答案:

答案 0 :(得分:1)

查询中有许多额外的括号,这些括号不是必需的。并且您可以将来自两个查询的条件组合在一起并使其成为单个查询。

见下面的查询:

SELECT * 
FROM 
    content 
WHERE 

    folder_id IN (SELECT 
                    folder_id 
                  FROM 
                    [content_folder_tbl] 
                WHERE 
                    parent_id in (158, 149)) AND 
    published = 1 AND 
    UPPER(content_title) NOT LIKE '%DESCRIPTION' AND 
    UPPER(content_title) NOT LIKE '%IMAGE' AND
    folder_id = 106 AND 
    content_type = 1 AND 
    published = 1 
ORDER BY 
    content_title