你好朋友, 第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 PatrickSQL查询
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 | pqrI 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 | pqrhow 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 MaryThen you insert a second row with the father and leave name empty
Name Parent John Mary <null> RobertThen you insert row 3 and 4 with Johns friend, Peter.
Name Parent John Mary <null> Robert Peter Elisabeth <null> PatrickNow 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 MaryThe 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 PatrickThe SQL query
SELECT Parent FROM Parents WHERE Name = 'John';will give the following result:
Parent Mary RobertHow 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 | pqrAnd 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查询。
发布评论