如何为这种情况编写sql查询。

编程入门 行业动态 更新时间:2024-10-28 06:29:56
本文介绍了如何为这种情况编写sql查询。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好朋友, 第1列|第2栏名称|父母姓名 ________ | _____________ john | xyz john | pqr

我在第一行插入名称和父名称但在第二行我只想在同一名称相同时插入父名称。(名字应该是空的)..我想输出是 -

第1列|第2栏名称|父母姓名 ________ | _____________ john | xyz | pqr

如何在sql server中为它编写查询...请帮帮我.. 谢谢

解决方案

你的问题毫无意义,因为它打破了关系数据库的基础。 如果没有某种引用或密钥,你就不能在表格中插入数据,这样你就可以把它拿回来了。 让我们说在你的例子中,你插入一行名字和父母。 姓名 父母 John Mary

然后你插入父亲的第二行并留空名称

姓名 父母 John Mary < null>罗伯特

然后你和约翰的朋友彼得一起插入第3行和第4行。

姓名 父母 John Mary < null> Robert Peter Elisabeth < null> Patrick

现在尝试创建一个SQL查询来获取两个男孩的父母姓名。 这可能有点棘手。对吗? SQL查询

SELECT 父 FROM Parents WHERE Name = ' John';

将给出以下结果:

家长 Mary

找不到父亲,因为在这种情况下姓名和家长没有连接。 所以当你存储数据时,你不能在没有任何参考的情况下悬挂一行来回拨。 表格应如下所示:

姓名 父母 John Mary John Robert Peter Elisabeth Peter Patrick

SQL查询

SELECT 父 FROM 父母 WHERE 名称= ' John';

将给出以下结果:

父母 Mary Robert

您如何决定在屏幕或报告上格式化和显示此数据与存储数据的方式不同。 我认为您对存储的方式感到困惑Excel表格中的数据以及如何将其存储在数据库中,但这只是猜测。

使用Row_number()并将其与John分组,以便输出看起来像这样

第0列|第1列|第2栏 Rownumber |名称| Parentsname _________ | _________ | _____________ 1 |约翰| xyz 2 |约翰| pqr

并使用案例陈述删除重复的约翰,如rownumber> 1然后''作为名称。 您可以使用CTE(公用表表达式)并在派生查询中执行case语句。 但最佳做法是在代码部分执行此操作前端

Hello friends,

column 1| column 2 Name | Parentsname ________|_____________ john | xyz john | pqr

I insert name and parent name in first row but in second row i want only parent name to be inserted if same name is same.(name should be empty)..I want output is-

column 1| column 2 Name | Parentsname ________|_____________ john | xyz | pqr

how to write query for it in sql server...please help me.. Thanks

解决方案

Your question makes no sense as it breaks the fundamentals of relational databases. You cannot insert data in a table without some kind of reference or key to so you can get it back. Let's say that in your example, that you insert a row with Name and Parent.

Name Parent John Mary

Then you insert a second row with the father and leave name empty

Name Parent John Mary <null> Robert

Then you insert row 3 and 4 with Johns friend, Peter.

Name Parent John Mary <null> Robert Peter Elisabeth <null> Patrick

Now try to create an SQL query to get both the parents name for either of the boys. That might be a bit tricky. Right? The SQL query

SELECT Parent FROM Parents WHERE Name = 'John';

will give the following result:

Parent Mary

The father cannot be found because Name and Parent are not connected in this case. So when you store the data, you cannot leave a row hanging without any reference to call it back. The table should look like this:

Name Parent John Mary John Robert Peter Elisabeth Peter Patrick

The SQL query

SELECT Parent FROM Parents WHERE Name = 'John';

will give the following result:

Parent Mary Robert

How you decide to format and present this data on a screen or report is i different thing from how you store the data. I think you have confused the way you store data in an Excel sheet with how you store it in a database, but that is just a guess.

Use the Row_number() and group it with John so the output looks like this

column 0 |column 1| column 2 Rownumber|Name | Parentsname _________|_________|_____________ 1 | john | xyz 2 | john | pqr

And use case statement to remove the repeating John like when rownumber>1 then '' as Name. You might use CTE(Common table Expression) and do the case statement in the derived query. But the best practice is to do it in the code part of front end

更多推荐

如何为这种情况编写sql查询。

本文发布于:2023-10-28 17:42:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这种情况   何为   sql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!