请帮忙!在SQL中选择查询 [英] Please help! Select query in SQL
问题描述
ID名称指定级别userid
15 Sh。 B.M. Babbar SDE-R2 00010104 ELE15
16 Sh。 Surinder AE-R2 0001010401 ELE16
17 Sh。 Roop Lal JE-R2 000101040101 ELE17
18 Sh。 Mansatinder JE-R3 000101040102 ELE18
328 testnews JE_R2 000101040103 JE_R2
329 testnews JE_R2 000101040104 JE_R2
330 testnew12 AE-R3 000101040105 AE-R3
331 testnews AE-R3 000101040106 AE-R3
这是我的employeeList表及上表下面的结果,显示sde(SDE-R2)作为父下AE作为儿童和AE(AE-R2)在JE下作为子孙desiganation。我想通过级别获得AE数据下的sde
i尝试此查询从EmployeeList中选择*,其中级别如'00010104%' 但显示所有记录
我尝试过:
i想要仅按父母数据显示子数据(SDE-R2 00010104)
16 Sh.Surinder AE-R2 0001010401 ELE16
我想通过父数据显示子数据(SDE-R2 00010104)
psuedo sql查询是:
从<<< ChildTable>>中选择*
内部联接<< ParentTable>>
on< childtable.FKID>> =<< ParentTable.PKID>>
其中<< ParentTable。<< level>>比如'00010104%'
我认为你应该永远不要使用选择*
在生产查询中,因为如果某些事情随着所涉及的所有表格的结构而改变,这将对您的数据路径产生负面影响。
选择*
适用于测试和数据转储,但这就是它,恕我直言。
因为你只想要此查询下面的级别应该完成这项工作:
SELECT *
FROM EmployeeList
WHERE LEVEL LIKE ' 00010104 __'
但是基于位置的层次结构键是我建议你重新考虑一下。
是的,永远不要在生产鳕鱼中使用SELECT *
e。
我认为解决方案1得到了错误的结束。根据您的注释,您只需要在查询中添加AND子句(并且如Slacker007所指出的那样,更正列列表)。例如
选择 ID,名称,名称,级别,用户ID
来自 EmployeeList 其中级别喜欢 ' 00010104%'
和名称喜欢 ' AE%'如果你还想包括你提到的子子名称,那么添加一个OR子句但是你需要将它括在括号中,例如
从 ID,名称,名称,级别,用户ID
> EmployeeList 其中级别喜欢 ' 00010104%'
和(指定喜欢 ' AE%' 或指定< span class =code-keyword> like ' JE%')
ID Name Designation Level userid 15 Sh. B.M. Babbar SDE-R2 00010104 ELE15 16 Sh. Surinder AE-R2 0001010401 ELE16 17 Sh. Roop Lal JE-R2 000101040101 ELE17 18 Sh. Mansatinder JE-R3 000101040102 ELE18 328 testnews JE_R2 000101040103 JE_R2 329 testnews JE_R2 000101040104 JE_R2 330 testnew12 AE-R3 000101040105 AE-R3 331 testnews AE-R3 000101040106 AE-R3
This my employeeList table and above following table result showing sde(SDE-R2) as parent under AE as child and AE(AE-R2) under JE as subchild desiganation. i want to get only sde under AE Data through by level
i try this query "Select * from EmployeeList where level like '00010104%'" but showing all records
What I have tried:
i want to show only child data by parent data (SDE-R2 00010104)
"
16 Sh. Surinder AE-R2 0001010401 ELE16
"
i want to show only child data by parent data (SDE-R2 00010104)
psuedo sql query is:
Select * from <<ChildTable>> Inner join <<ParentTable>> on <childtable.FKID>> = <<ParentTable.PKID>> Where <<ParentTable.<<level>> like '00010104%'
I think it important to mention that you should never useSelect *
in a production query, as that can have negative implications for your data path in the future if something were to change with the structure of all the tables involved.
Select *
is good for testing and for data dumps, but that is about it, IMHO.
Since you only want the level below this query should do the job:
SELECT * FROM EmployeeList WHERE LEVEL LIKE '00010104__'
But having a position based hierarchy key is something I'd recommend that you rethink.
And yes, never useSELECT *
in production code.
I think solution 1 got the wrong end of the stick. Based on your comment you just need to add and AND clause to your query (and as pointed out by Slacker007, correct your column list). E.g.
Select ID, Name, Designation, Level, userid from EmployeeList where level like '00010104%' and Designation like 'AE%'If you also want to include the "sub-child" designation you mention then add an OR clause but you will need to enclose it in brackets e.g.
Select ID, Name, Designation, Level, userid from EmployeeList where level like '00010104%' and ( Designation like 'AE%' or Designation like 'JE%' )
这篇关于请帮忙!在SQL中选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!