我有一张这样的表:
tblMembers
ID Credit Is_Member
--- --- ---
1 45 True
2 20 False
3 25 True
4 -10 True
5 -5 False
6 13 True
如何创建一个视图,显示<最低正值值信用和 Is_Member =“True”的记录?
答案 0 :(得分:2)
select min(credit)
from tblMembers
where Is_Member = 'true'
and credit > 0
...如果这是作业,请将其标记为(因为答案可能不同)。
修改强>
没有正确阅读问题。
select * from tblMembers
where credit = (select min(credit)
from tblMembers
where credit > 0
and Is_Member = 'true')
and Is_Member = 'true'
...您需要在两个位置检查Is_Member ='true',因为最低的信用评分可能不是会员(这将不会产生任何结果)并且非会员和会员可能会分享最低分数但仅会员应该出示。
答案 1 :(得分:2)
可能有多条记录符合此标准......
SELECT *
FROM tblMembers
WHERE Credit = (
SELECT MIN(Credit)
WHERE Credit > 0
AND Is_Member = 'True'
)
答案 2 :(得分:2)
您使用的查询类似于:
SELECT *
FROM tblMembers
WHERE Credit = (SELECT MIN(Credit)
FROM tblMembers
WHERE Is_Member = TRUE AND Credit > 0)
AND Is_Member = TRUE
请注意,可以为最低正值绑定多行,因此您可能会获得多个结果。
答案 3 :(得分:1)
SELECT * FROM tblMembers
WHERE Credit =
(SELECT MIN(Credit) FROM tblMembers WHERE Credit > 0 AND Is_Member = 'True')
AND Is_Member = 'True'
请注意,如果您有多个具有相同(最低)点数的行,则会返回多行。
答案 4 :(得分:1)
您可能有多条符合您标准的记录。如果您只想要一行:
create view PositiveCreditMember
as
select top 1
ID
, credit
, is_member
from
tblMembers
where
credit in (
select
min(Credit)
from
tblMembers
where
credit > 0
and is_member = 'True'
)
and is_member = 'True'
order by ID
答案 5 :(得分:0)
我错过了什么吗?这似乎很简单:
select min(Credit) as Credit
from tblMembers
where Is_Member = 'True' and Credit >= 0
答案 6 :(得分:0)
SELECT MIN(Credit), ID FROM tblMembers WHERE Is_Member="True"
GROUP BY ID HAVING MIN(Credit) > 0
但是,我认为我可能会过度简化这一点。使用风险自负。
答案 7 :(得分:0)
或
select * from credit
where is_member = 'true'
and credit > 0
order by credit
limit 1
又名
select top 1 * from credit
where is_member = 'true'
and credit > 0
order by credit
取决于您的SQL风格。这只给出了一条记录;正如其他人所说,可能有多个记录具有最小值。
答案 8 :(得分:0)
怎么样:
CREATE VIEW vwXYZ
AS
SELECT TOP 1 *
FROM tblMembers
WHERE [Is_Member] = 1
AND Credit > 0
ORDER BY Credit;
这消除了子查询。
答案 9 :(得分:0)
如果您想随机匹配其中一行:
select top 1 * from tblMembers where Is_Member = 1 order by credit
或者如果你想要所有匹配的行:
select top 1 with ties * from tblMembers where Is_Member = 1 order by credit