Sql server 在SQL Server中搜索同一列同一表
我试着 我想要“选择uıd=5行”,但不是我想要在表1和UID中选择uıd=23行 表1Sql server 在SQL Server中搜索同一列同一表,sql-server,Sql Server,我试着 我想要“选择uıd=5行”,但不是我想要在表1和UID中选择uıd=23行 表1 select * FROM TABLE 1 where UID = 5 if not Exist where UID=23 我对SQK非常陌生,所以我不确定我应该做什么来实现我想要的。任何帮助都将不胜感激。我不知道您为什么要这样做,但有几种方法可以做到这一点。这是一个: Key | Short_text | UID | Boolean_value **************************
select * FROM TABLE 1 where UID = 5
if not Exist where UID=23
我对SQK非常陌生,所以我不确定我应该做什么来实现我想要的。任何帮助都将不胜感激。我不知道您为什么要这样做,但有几种方法可以做到这一点。这是一个:
Key | Short_text | UID | Boolean_value
******************************************
Name | John | 23 | null
Male | NULL | 23 | true
Name | Ben | 45 | null
Male | NULL | 45 | true
你能找一个以英语为母语的人帮你重新写你的问题吗?我不知道你想说什么。你说得对,但我什么都不能说!我有一个表,我想写一行,其中id=5,如果我的表没有id=5的值,我应该取id=0(表中id的默认值)行。我搜索这个sql代码这里是一个很好的起点@BegumCH,你能澄清你问数据库的问题吗?在我看来,您是在问“我想要用户的密钥、短文本、UID和UID为5的用户的布尔值。如果UID 5不存在,请为UID 23提供相同的信息。”是否正确?这里我无法解决的是,显然每个用户有两行。通常,值“Male | NULL | 23 | true”将是第一行的属性。我认为表1应该是这样的:
UID | Name | IsMale |
23 | John | True |
:避免在ORDER BY子句中指定整数作为select列表中列的位置表示。[…]更改select列表,例如更改列顺序或添加新列,将需要修改ORDER BY子句以避免意外的结果。谢谢marc_。这是您的兴趣。我不明白(x)x什么表格?专栏?我喜欢这个网站,但当我发帖和试图编辑时,它总是抛出一个错误。今天早上我试着编辑了三次,但都失败了。上面的代码并不能完全解决问题,但已经很接近了。case语句后面需要“as RN”,然后是逗号和*.TT,没错。我在赶时间的时候这样做。公平地说,我也没有在选择中使用*。我希望这段代码不会被复制粘贴到产品中,而是更多地用作指导。话虽如此,我还是会尽量保持代码的干净。
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
/* 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
-- 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