如何让 MS SQL 创建具有唯一 ID 的视图?

发布于 2024-12-23 04:14:17 字数 1717 浏览 1 评论 0原文

背景:

我首先被要求做一个使用mysql作为数据库的网站,完成并发送后,客户要求我将其转换为mssql,以及3层。

我完成了在服务器之间传输数据的类和函数,因此 3 层位大约完成了一半。我现在正在努力解决的是MsSQL。

问题:

该问题的 MySQL 版本如下所示:

select a.id as a_id, a.first_name, a.last_name, a.agent_code,
       b.id as b_id, b.user_id, b.status_admin 
from tbl_user a 
  inner join tbl_testimonia b 
    on a.id = b.user_id 
where b.status_admin=0  
group by a.id
order by b.id desc

它的作用是返回尚未批准的推荐,并分组到每个用户;


我陷入困境:

我无法将其转换为 MSSQL

我最好的尝试:

  • 创建视图;

如:

    create view test as
    (
    select a.id as a_id, a.first_name, a.last_name, a.agent_code,
    b.id as b_id, b.user_id,  b.status_admin 
    from tbl_user a inner join tbl_testimonia b on a.id = b.user_id 
    where b.status_admin=0 and a.id in  
    (
    select a.id from tbl_user a 
    inner join tbl_testimonia b 
    on a.id = b.user_id
    where b.status_admin=0 
    group by a.id
    )
    )

这选择了我想要的内容,但是创建视图的id字段无法分组,这意味着id字段不是唯一的并且可以具有相同的值。

我想到目前为止我的问题是,如何在视图中唯一选择 id 字段? 当然我可以用 PHP 做到这一点,但一个月前我第一次遇到这个问题时我就可以做到这一点。

我已经尝试找到答案有一段时间了,但似乎找不到我的问题

  • 编辑特有的答案:MSSQL 不起作用,因为即使子查询选择唯一的 id,in主查询中的语句使此无关
  • 编辑:示例输出::~(在 MySql 中,第三个字段被省略)

    <前><代码>| a_id |第一个 |最后 N |代理代码 | b_id |用户 ID |状态管理员 | +------------------------------------------------ ----------------------------------+ | 32 | 32 fn1 | ln1 | AC123213 | 14 | 14 32 | 32 0 | | 41 | 41 fn2 | ln2 | 12345678 | 15 | 15 41 | 41 0 | | 32 | 32 fn1 | ln1 | AC123213 | 16 | 16 32 | 32 0 |
  • 编辑:问题已解决非常感谢Lieven的简单答案

Background:

i was first asked to do a website using mysql as the database, after that was done and sent, the clients asked me to convert it to mssql, and 3 tiers.

i did the classes and functions for transferring data between the servers, so the 3 tiers bit is about halfway done. the thing i'm struggling with now is the MsSQL.

Question:

the MySQL version of the question looks like this:

select a.id as a_id, a.first_name, a.last_name, a.agent_code,
       b.id as b_id, b.user_id, b.status_admin 
from tbl_user a 
  inner join tbl_testimonia b 
    on a.id = b.user_id 
where b.status_admin=0  
group by a.id
order by b.id desc

what it does is return testimonials that aren't approved yet, grouped to each user;


Where I am stuck:

i cant convert this to MSSQL

My best try:

  • Create view;

as in:

    create view test as
    (
    select a.id as a_id, a.first_name, a.last_name, a.agent_code,
    b.id as b_id, b.user_id,  b.status_admin 
    from tbl_user a inner join tbl_testimonia b on a.id = b.user_id 
    where b.status_admin=0 and a.id in  
    (
    select a.id from tbl_user a 
    inner join tbl_testimonia b 
    on a.id = b.user_id
    where b.status_admin=0 
    group by a.id
    )
    )

this selects what i want, but the id field of the create view cant be grouped, meaning the id field is not unique and can have the same value.

I guess my question up to this point is, how do i uniquely select the id field in the view?
sure i can do it in PHP, but i could have done that a month ago when i first encountered the problem.

I've been trying to find the answer for a while now but cant seem to find the answer unique to my question

  • edit: the MSSQL doesn't work because even though the subquery is selecting unique ids the in statement in the main query makes this irrelevent
  • edit: example output::~ (in MySql the 3rd field is ommited)

    | a_id  |  firstN | LastN |  agent_code  |   b_id  |  user_id |status_admin|
    +--------------------------------------------------------------------------+
    | 32    |  fn1    | ln1   |  AC123213    |   14    |   32     |   0        |
    | 41    |  fn2    | ln2   |  12345678    |   15    |   41     |   0        |
    | 32    |  fn1    | ln1   |  AC123213    |   16    |   32     |   0        |
    
  • edit: QUESTION SOLVED a big thx to Lieven for the simple answer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2024-12-30 04:14:17

正如您已经解释过的那样,与 SQL Server 不同,MySQL 允许使用像这样的未聚合表达式进行分组,

SELECT  *
FROM    mytable
GROUP BY
        column

因为 GROUP BY 中的未聚合表达式返回每个组中的任意记录,并且如果值不同,则不应使用,您应该能够在 SQL Server 中使用以下语句作为等效语句

select a.id as a_id
       , MIN(a.first_name)
       , MIN(a.last_name)
       , MIN(a.agent_code)
       , MIN(b.id as b_id)
       , MIN(b.user_id)
       , MIN(b.status_admin)
from   tbl_user a 
       inner join tbl_testimonia b on a.id = b.user_id 
where  b.status_admin=0  
group by 
       a.id
order by 
       b.id desc

As you have already explained yourself, unlike SQL Server, MySQL allows grouping by with unaggregated expressions like this

SELECT  *
FROM    mytable
GROUP BY
        column

As an unaggregated expression in GROUP BY returns an arbitrary record from each group and is not supposed to be used if the values vary you should be able to use the following statement as an equivalent in SQL Server

select a.id as a_id
       , MIN(a.first_name)
       , MIN(a.last_name)
       , MIN(a.agent_code)
       , MIN(b.id as b_id)
       , MIN(b.user_id)
       , MIN(b.status_admin)
from   tbl_user a 
       inner join tbl_testimonia b on a.id = b.user_id 
where  b.status_admin=0  
group by 
       a.id
order by 
       b.id desc
末骤雨初歇 2024-12-30 04:14:17

如果你想有不重复的ID,你可以

select DISTINCT a.id from tbl_user a 
inner join tbl_testimonia b 
  on a.id = b.user_id where b.status_admin=0 

在你的子查询中

If you want to have non repetitive ids you can

select DISTINCT a.id from tbl_user a 
inner join tbl_testimonia b 
  on a.id = b.user_id where b.status_admin=0 

in your sub query

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文