在SQL Server中搜索同一列相同的表

时间:2016-01-26 21:39:55

标签: sql-server

我试试

select * FROM TABLE 1 where UID = 5


if not Exist where UID=23

我想要“选择uıd= 5行”但不是我想在表1和UID中选择uıd= 23行

表1

 Key | Short_text | UID | Boolean_value
******************************************
Name | John       | 23  | null
Male | NULL       | 23  | true
Name | Ben        | 45  | null
Male | NULL       | 45  | true

我对SQK很新,所以我不确定应该做些什么来实现我想要的。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

我不确定你为什么这样做,但有几种方法可以做到这一点。这是一个:

SELECT TOP 1 
    *
FROM 
    (SELECT  
         CASE WHEN UID = 5 THEN 1 ELSE 0 END
     FROM    
         TABLE1
     WHERE  
         UID IN (5, 23) ) X
ORDER BY 1

答案 1 :(得分:0)

/* Table 1, as given
Key | Short_text | UID | Boolean_value
******************************************
Name | John       | 23  | null
Male | NULL       | 23  | true
Name | Ben        | 45  | null
Male | NULL       | 45  | true
*/

CREATE TABLE #TABLE1
(
    [Key]          VARCHAR(10)
   , Short_text    VARCHAR(10)
   , UID           INT
   , Boolean_value VARCHAR(5)
)
INSERT INTO #TABLE1
( 
     [Key]
   , Short_text
   , UID
   , Boolean_value -- using 'null' instead of NULL here because the other value is 'true'.
)                  -- There are no Boolean keywords like TRUE or FALSE in SQL Server. 
-- This VALUES syntax assumes the use of SQL Server 2008R2 or greater.
VALUES
   ('Name', 'John', 23, 'null'),   
   ('Male',  NULL,  23, 'true'),
   ('Name', 'Ben',  45, 'null'),   
   ('Male',  NULL,  45, 'true')


-- @paulbarbin, this query always returns 0 (for Selector) if UID=5 doesn't exist, else always 1 (for Selector)
SELECT TOP 1 
    *
FROM 
    (SELECT  
         CASE WHEN UID = 5 THEN 1 ELSE 0 END AS Selector -- Column name or alias required here
     FROM    
         #TABLE1
     WHERE  
         UID IN (5, 23) ) X
ORDER BY 1  

-- Perhaps this.  A bit verbose, but the intent is clearly shown...
DECLARE @DesiredUID INT =  5
DECLARE @DefaultUID INT = 23
IF EXISTS  (SELECT * FROM #TABLE1 WHERE UID = @DesiredUID)
   SELECT TOP (2) 
      [Key]
    , Short_text
    , UID
    , Boolean_value 
   FROM #TABLE1 
   WHERE UID = @DesiredUID
   ORDER BY UID, ISNULL(Short_text,'zzzzzzzzzz')  -- Because NULL sorts ahead of 'John'
ELSE
   SELECT TOP (2) 
      [Key]
    , Short_text
    , UID
    , Boolean_value 
   FROM #TABLE1 
   WHERE UID = @DefaultUID
   ORDER BY UID, ISNULL(Short_text,'zzzzzzzzzz')

/* When @DesiredUID = 5 and @DefaultUID = 23
Key   Short_text  UID  Boolean_value
----  ----------  ---  -------------
Name  John         23  null         
Male  NULL         23  true         
*/

-- Cleanup
DROP TABLE #TABLE1

答案 2 :(得分:0)

-- Since the OP is new to SQL,
-- From the OP: "... I have one table and I want one row and write where id=5 ,
--               if my table have not id=5 value I should take id=0(default value for id in my table) row . ...]  
-- This statement suggests a normalized TABLE1.
-- From that perspective, consider this:

 /* Table1, Proposed structure
UID | Name | Short_text | Gender | Boolean_value
*************************************************************
23  | John | John       |  Male  | 1
45  | Ben  | Ben        |  Male  | 1
*/

CREATE TABLE #TABLE1
(
     UID           INT NOT NULL PRIMARY KEY CLUSTERED -- Assume UID to be the Primary Key for TABLE1
   , Name          VARCHAR(10)
   , Short_text    VARCHAR(10)
   , Gender        VARCHAR(10)
   , Boolean_value BIT NOT NULL DEFAULT 0  -- Typical definition for Boolean usage of BIT
)
INSERT INTO #TABLE1
( 
     UID            
   , Name          
   , Short_text    
   , Gender        
   , Boolean_value          
)     
-- This VALUES syntax assumes the use of SQL Server 2008R2 or greater.
VALUES
   -- (UID, Name,   Short_text, Gender, Boolean_value)
      (23, 'John', 'John',      'Male', 1),
      (45, 'Ben',  'Ben',       'Male', 1)

-- A bit verbose, but the intent is clearly shown...
DECLARE @DesiredUID INT =  5  
DECLARE @DefaultUID INT = 23  -- When the default is zero, no rows are returned from this demo table.
IF EXISTS  (SELECT * FROM #TABLE1 WHERE UID = @DesiredUID)
   SELECT TOP (1) 
      UID          
    , Name         
    , Short_text   
    , Gender       
    , Boolean_value
   FROM #TABLE1 
   WHERE UID = @DesiredUID
   ORDER BY UID -- Though not needed in this case (because at most only one row will be returned,)
                --    ORDER BY should always be used with TOP.
ELSE
   SELECT TOP (1) 
      UID          
    , Name         
    , Short_text   
    , Gender       
    , Boolean_value
   FROM #TABLE1 
   WHERE UID = @DefaultUID
   ORDER BY UID


/* When @DesiredUID = 5 and @DefaultUID = 23
UID  Name  Short_text  Gender  Boolean_value
---  ----  ----------  ------  -------------
 23  John  John        Male                1    
*/

/* When @DesiredUID = 5 and @DefaultUID = 0
UID  Name  Short_text  Gender  Boolean_value
---  ----  ----------  ------  -------------

*/

-- Cleanup
DROP TABLE #TABLE1