Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQLÖÐEXISTSµÄÓ÷¨


±ÈÈçÔÚNorthwindÊý¾Ý¿âÖÐ  
  ÓÐÒ»¸ö²éѯΪ  
  SELECT   c.CustomerId,   CompanyName  
  from   Customers   c  
  WHERE   EXISTS(  
              SELECT   OrderID   from   Orders   o  
              WHERE   o.CustomerID   =   cu.CustomerID)  
   
  ÕâÀïÃæµÄEXISTSÊÇÈçºÎÔË×÷ÄØ£¿×Ó²éѯ·µ»ØµÄÊÇOrderId×ֶΣ¬¿ÉÊÇÍâÃæµÄ²éѯҪÕÒµÄÊÇCustomerIDºÍCompanyName×ֶΣ¬ÕâÁ½¸ö×ֶο϶¨²»ÔÚOrderIDÀïÃæ°¡£¬ÕâÊÇÈçºÎÆ¥ÅäµÄÄØ£¿
 
EXISTSÓÃÓÚ¼ì²é×Ó²éѯÊÇ·ñÖÁÉٻ᷵»ØÒ»ÐÐÊý¾Ý£¬¸Ã×Ó²éѯʵ¼ÊÉϲ¢²»·µ»ØÈκÎÊý¾Ý£¬¶øÊÇ·µ»ØÖµTrue»òFalse
 
EXISTS
Ö¸¶¨Ò»¸ö×Ó²éѯ£¬¼ì²âÐеĴæÔÚ¡£
Óï·¨
EXISTS subquery
²ÎÊý
subquery
ÊÇÒ»¸öÊÜÏÞµÄ SELECT Óï¾ä (²»ÔÊÐíÓÐ COMPUTE ×Ó¾äºÍ INTO ¹Ø¼ü×Ö)¡£Óйظü¶àÐÅÏ¢£¬Çë²Î¼û SELECT ÖÐÓйØ×Ó²éѯµÄÌÖÂÛ¡£
½á¹ûÀàÐÍ
Boolean
½á¹ûÖµ
Èç¹û×Ó²éѯ°üº¬ÐУ¬Ôò·µ»Ø TRUE¡£
ʾÀý
A. ÔÚ×Ó²éѯÖÐʹÓà NULL ÈÔÈ»·µ»Ø½á¹û¼¯
Õâ¸öÀý×ÓÔÚ×Ó²éѯÖÐÖ¸¶¨ NULL£¬²¢·µ»Ø½á¹û¼¯£¬Í¨¹ýʹÓà EXISTS ÈÔȡֵΪ TRUE¡£
USE Northwind
GO
SELECT CategoryName
from Categories
WHERE EXISTS (SELECT NULL)
ORDER BY CategoryName ASC
GO
B. ±È½ÏʹÓà EXISTS ºÍ IN µÄ²éѯ
Õâ¸öÀý×ӱȽÏÁËÁ½¸öÓïÒåÀàËƵIJéѯ¡£µÚÒ»¸ö²éѯʹÓà EXISTS ¶øµÚ¶þ¸ö²éѯʹÓà IN¡£×¢ÒâÁ½¸ö²éѯ·µ»ØÏàͬµÄÐÅÏ¢¡£
USE pubs
GO
SELECT DISTINCT pub_name
from publishers
WHERE EXISTS
    (SELECT *
    from titles
    WHERE pub_id = publishers.pub_id
    AND type = \'business\')
GO
-- Or, using the IN clause:
USE pubs
GO
SELECT distinct pub_name
from publishers
WHERE pub_id IN
    (SELECT pub_id
    from titles
    WHERE type = \'business\')
GO
ÏÂÃæÊÇÈÎÒ»²éѯµÄ½á¹û¼¯£º
pub_name                      &nbs


Ïà¹ØÎĵµ£º

[SQL Server2008]SQL Server ÖÐ.Óë::µÄÇø±ð(ת)

SQL ServerÖе÷Ó÷½·¨Ê±£¬¾­³£Óöµ½.»òÊÇ::Á½ÖÖµ÷Ó÷½·¨£¬ÆäÖÐ. ÊÇʵÀýµ÷Ó÷½·¨£¬::ÊǾ²Ì¬µ÷Ó÷½·¨¡£Èç¹û´ó¼Ò¶ÔC#Óï·¨ÊìϤµÄ»°£¬. ¶ÔÓ¦C#Öе÷ÓÃʵÀý·½·¨£¬:: ¶ÔÓ¦C#ÖеÄstatic·½·¨¡£
ÏÂÃæ¸ø³öÒ»¸öSQL ServerµÄʵÀý£¬ÏÂÃæµÄSQLÔËÐÐÔÚ2008»·¾³Ï£º
DECLARE @g geography;
SET @g = geography::Parse('LINESTRING(-122.360 ......

sql in תΪexists

-- MySQL dump 10.10
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 5.0.22-community-nt
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*! ......

sql isnullº¯Êý


ISNULL
²éѯÊý¾ÝʱÈô×Ö¶ÎÖµÊÇNULL£¬ÔòʹÓÃʹÓÃÖ¸¶¨µÄÌæ»»ÖµÌæ»» NULL¡£
Óï·¨
ISNULL ( check_expression , replacement_value )
²ÎÊý
check_expression
½«±»¼ì²éÊÇ·ñΪ NULLµÄ±í´ïʽ¡£check_expression ¿ÉÒÔÊÇÈκÎÀàÐ͵ġ£
replacement_value
ÔÚ check_expression Ϊ NULLʱ½«·µ»ØµÄ±í´ïʽ¡£replacement_value ± ......

Pl/sql ±à³ÌÖ®case whenµÄÓ÷¨

Pl/sql ±à³ÌÖ®case whenµÄÓ÷¨£º£¨t3.LOCAL_BF_KND='111000' -- ±¾Ô»ù±¾ÔÂ×â·Ñ£©
select sum(t1.FAV_FEE) from bi_rw.FO_A_2FAV_M_G t1, bi_rw.D_BILLFEE_KND t3 where t1.BILLFEE_KND=t3.BILLFEE_KND(+) and t3.LOCAL_BF_KND='111000'  --1310557.76
 
Èç¹û²»±ãÓÚ°ÑÌõ¼þt3.LOCAL_BF_KND='111000' дÔÚwhereÌ ......

SQLÓï¾äÊÕ¼¯

Ò»¡¢»ù´¡
1¡¢ËµÃ÷£º´´½¨Êý¾Ý¿â
CREATE DATABASE database-name
2¡¢ËµÃ÷£ºÉ¾³ýÊý¾Ý¿â
drop database dbname
3¡¢ËµÃ÷£º±¸·Ýsql server
--- ´´½¨ ±¸·ÝÊý¾ÝµÄ device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- ¿ªÊ¼ ±¸·Ý
BACKUP DATABASE pubs TO testBack
4¡¢Ëµ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ