SQL存在且不存在

时间:2017-07-19 16:53:45

标签: mysql sql database

由于我是SQL的新手,我在理解NOT EXISTS+----+----------------+--------------------+ | id | nome | email | +----+----------------+--------------------+ | 1 | João da Silva | joao@dasilva.com | | 2 | Frederico José | fred@jose.com | | 3 | Alberto Santos | alberto@santos.com | | 4 | Renata Alonso | renata@alonso.com | | 5 | Paulo da Silva | paulo@dasilva.com | +----+----------------+--------------------+ 方面遇到了一些麻烦。

我有这些表格:

表Aluno:

+----+----------+----------+---------------------+---------+
| id | aluno_id | curso_id | data                | tipo    |
+----+----------+----------+---------------------+---------+
|  1 |        1 |        1 | 2013-11-25 16:16:05 | PAGA_PF |
|  2 |        2 |        1 | 2013-05-25 16:16:25 | PAGA_PJ |
|  3 |        3 |        3 | 2013-07-21 16:16:30 | PAGA_PF |
|  4 |        4 |        4 | 2013-11-15 16:15:35 | PAGA_PK |
|  5 |        2 |        2 | 2012-01-04 00:00:00 | PAGA_PJ |
+----+----------+----------+---------------------+---------+

表Matricula

select a.nome from Aluno a where not exists(select m.id from Matricula m where m.aluno_id = a.id and m.data < now() - interval 46 month);

运行此查询:

+----------------+
| nome           |
+----------------+
| João da Silva  |
| Renata Alonso  |
| Paulo da Silva |
+----------------+

我得到了这个结果:

NOT EXISTS

问题是:如果子查询的select语句返回的行数超过TRUE0子查询如何{{1}}?

1 个答案:

答案 0 :(得分:1)

弗雷德里科和阿尔贝托的<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ListView Name="listView" Width="{Binding ElementName=sampleTree, Path=Width}"> <ListView.View> <GridView x:Name="gridView" AllowsColumnReorder="False"> <GridViewColumn Header="Subject" x:Name="col1" Width="105"/> <GridViewColumn Header="Module" x:Name="col2" Width="105"/> <GridViewColumn Header="Component" x:Name="col3" Width="105"/> <GridViewColumn Header="Task" x:Name="col4" Width="105"/> <GridViewColumn Header="State" x:Name="col5" Width="105"/> </GridView> </ListView.View> </ListView> <TreeView Grid.Row="1" x:Name="sampleTree" ItemsSource="{Binding AssessmentFramework}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type models:TreeNode}" ItemsSource="{Binding Items}"> <Border BorderBrush="Black" BorderThickness="1" Padding="3" Margin="1"> <TextBox Text="{Binding Name}" IsReadOnly="{Binding Path=Id, Converter={StaticResource TextBoxEnableConverter}}" FontWeight="Bold" BorderThickness="0" Background="Transparent"> <TextBox.Width> <MultiBinding Converter="{StaticResource ResourceKey = ColumnToTreeNodeWidthConverter}"> <Binding ElementName="col1" Path="Width"/> <Binding ElementName="col2" Path="Width"/> <Binding ElementName="col3" Path="Width"/> <Binding ElementName="col4" Path="Width"/> <Binding ElementName="col5" Path="Width"/> <Binding Path="Lvl"/> </MultiBinding> </TextBox.Width> <i:Interaction.Behaviors> <behaviors:TextBoxDoubleClickBehavior/> </i:Interaction.Behaviors> </TextBox> </Border> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView> </Grid> 子查询是正确的,而其他三个则是假的。这是正确的,因为Matricula表中没有行与Aluno 1,4或5相关联,并且在过去46个月中有一个日期。