• Union用法


    一、Union用法:

    SQL Statement 1
    UNION ALL
    SQL Statement 2

    (1)
    Select * from (
    (
    Select Top 10 n.* ,m.Menu_Name from  News as n
    inner join Menu as m On n.Menu_Pk=m.Menu_Pk where n.Menu_Pk='21'
    )
    union all
    (
    Select Top 10 n.* ,m.Menu_Name from  News as n
    inner join Menu as m On n.Menu_Pk=m.Menu_Pk where n.Menu_Pk='22'
    )
    )as t1
    Order by News_Time Desc

    (2)

    Select * from
    (
    Select * from (Select Top 10 n.* ,m.Menu_Name from  News as n
    inner join Menu as m On n.Menu_Pk=m.Menu_Pk where n.Menu_Pk='21'  Order by News_Time desc

    ) as ti

    union all
    Select * from

    (Select Top 10 n.* ,m.Menu_Name from  News as n
    inner join Menu as m On n.Menu_Pk=m.Menu_Pk where n.Menu_Pk='22'

    )as t2
     )as t
    Order by News_Time Desc

    Union有两种:union ,union all区别是是否记录出现重复,UNION ALL只是简单的将两个结果合并后就返回。这样,如果返回的两个结果集中有重复的数据,那么返回的结果集就会包含重复的数据了。

    注意:如果想对用Union写的语句查询出的结果集进行排序,在最后排序。

     /// <summary>
            /// 根据栏目kind查询出各个栏目的最新十条新闻并且是审核通过///////////////////////////////创建缓存
            /// </summary>
            public DataTable GetKindTenNews(int Kind)
            {
                StringBuilder strSql = new StringBuilder();
                string sts = "审核通过";
                    strSql.Append("Select * from (");
                    if (Kind == 3)
                    {
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=21 And News_Check='" + sts + "') ");
                        strSql.Append(" Union ");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=22 And News_Check='" + sts + "') ");
                        strSql.Append(" Union ");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=23 And News_Check='" + sts + "' ) ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=25 And News_Check='" + sts + "' ) ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=26  And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=27 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=28 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=29 And News_Check='" + sts + "') ");
                    }
                    else if (Kind == 4)
                    {
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=30 And News_Check='" + sts + "') ");
                        strSql.Append(" Union ");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=36 And News_Check='" + sts + "') ");
                        strSql.Append(" Union ");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=37 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=38 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=39 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=40 And News_Check='" + sts + "' ) ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=41 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk And News_Check='" + sts + "'");
                        strSql.Append("where n.Menu_Pk=42 ) ");
                    }
                    else if (Kind == 5)
                    {
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=43 And News_Check='" + sts + "') ");
                        strSql.Append(" Union ");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=44 And News_Check='" + sts + "') ");
                        strSql.Append(" Union ");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=45 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=46 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=47 And News_Check='" + sts + "') ");
                        strSql.Append(" Union");
                        strSql.Append("( Select Top 10 n.* ,m.Menu_Name from ");
                        strSql.Append("News as n inner join Menu as m ");
                        strSql.Append("On n.Menu_Pk=m.Menu_Pk ");
                        strSql.Append("where n.Menu_Pk=48 And News_Check='" + sts + "') ");
                    }
                strSql.Append(" ) as t1 ");
                strSql.Append("Order By News_Time Desc ");
                dp.CommandText = strSql.ToString();
                dp.Parameters.Add("News_Kind", Kind);
                DataTable Studt = dp.ExecuteDataTable();
                return Studt;
            }

  • 相关阅读:
    FZU-2087 统计树边(最小生成树)
    HDU-1599 find the mincost route(floyd求最小环)
    BZOJ-1191 [HNOI2006]超级英雄Hero(二分图匹配)
    FZU-2020 组合(Lucas定理)
    FZU-2232 炉石传说(二分图匹配)
    NOIP2016模拟 拼接mf(模拟)
    2016年11月10日00:26:08
    BZOJ2986 Non-Squarefree Numbers
    BZOJ3624 [Apio2008]免费道路
    BZOJ3224 Tyvj 1728 普通平衡树
  • 原文地址:https://www.cnblogs.com/lykbk/p/43564354rthtghtght.html
Copyright © 2020-2023  润新知