我有这张桌子(这是一个简短的版本)
item_id computer_type operating_system
1 PC UNIX
2 DESKTOP OSX
3 LAPTOP WINDOWS
4 DESKTOP UNIX
5 PC OSX
6 PC WINDOWS
我如何使用SQL来确定“桌面”的数量?表中的计算机与' unix'?
一起运行答案 0 :(得分:0)
Select count(1)
from table
where computer_type = 'DESKTOP'
and operating_system = 'UNIX'
答案 1 :(得分:0)
SELECT computer_type,
operating_system,
count(*)
FROM computers
WHERE computer_type = 'DESKTOP'
AND operating_system = 'UNIX'
GROUP BY
computer_type,
operating_system;
这假定COMPUTER_TYPE
和OPERATING_SYSTEM
列的类型为VARCHAR2
(如果使用DESCRIBE computers;
命令,则可以看到)。如果他们的类型为CHAR
,那么他们将使用空格字符进行右边填充,您可以使用它:
SELECT computer_type,
operating_system,
count(*)
FROM computers
WHERE RTRIM( computer_type ) = 'DESKTOP'
AND RTRIM( operating_system ) = 'UNIX'
GROUP BY
computer_type,
operating_system;
或者,您可以将字符串文字填充到适当的长度:
SELECT computer_type,
operating_system,
count(*)
FROM computers
WHERE computer_type = 'DESKTOP '
AND operating_system = 'UNIX '
GROUP BY
computer_type,
operating_system;